Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有这样的方法:
def sum *numbers numbers.inject{|sum, number| sum += number} end
我如何能够将数组作为数字传递?
ruby-1.9.2-p180 :044 > sum 1,2,3 #=> 6 ruby-1.9.2-p180 :045 > sum([1,2,3]) #=> [1, 2, 3]
请注意,我无法更改 sum 方法以接受数组。
调用方法时只是放一个splat?
sum(*[1,2,3])
你是这个意思吗?
@Dogbert 是第一个