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

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -