ruby - How do I pass an array to a method that accepts an attribute with a splat operator? -



ruby - How do I pass an array to a method that accepts an attribute with a splat operator? -

if have method like:

def sum *numbers numbers.inject{|sum, number| sum += number} end

how able pass array numbers?

ruby-1.9.2-p180 :044 > sum 1,2,3 #=> 6 ruby-1.9.2-p180 :045 > sum([1,2,3]) #=> [1, 2, 3]

note can't alter sum method take array.

just set splat when calling method?

sum(*[1,2,3])

ruby splat

Comments

Popular posts from this blog

java - How to pass parameters between Request-Scoped Controllers? -

actionscript 3 - Flex: moving a point with rotation doesnt change the point XY? -

string - what is the difference between "some" == "some\0" and strcmp("some","some\0") in c++? -