Back

ruby - 方法调用中的 星号 (star, splat operator)

发布时间: 2018-08-01 09:54:00

说来惭愧。这个知识真的没用到过。 

https://stackoverflow.com/questions/918449/what-does-the-unary-operator-do-in-this-ruby-code

在ruby 中, * 也叫 splat operator, 专门为了方便的把一个 数组,作为多个参数传递给某个函数。

def hi name1, name2, name3
  puts "hi #{name1}, #{name2}, #{name3}"
end

a = [1,2,3]

然后,我们运行: $ hi *a 就可以了。 输出: hi 1,2,3

Back