ruby - method parameter single star double start 方法参数中的单* 与 ** 的参数 splat operator

访问量: 86

refer to:
https://www.freecodecamp.org/news/rubys-splat-and-double-splat-operators-ceb753329a78/

*:  splat operator   不能作用于hash

**:  double splat operator   ruby 2.0 以后引入的,可以作用于 hash

例子:

def dubSplat(a, *b, **c)
  puts c
end

dubSplat(1,2,3, 4, a: 40, b: 50)     #{:a=>40, :b=>50}

再看例子:

first, *rest, last = ["a", "b", "c", "d"]
p first # "a"
p rest # ["b", "c"]
p last # "d"

订阅/RSS Feed

Subscribe