|
Earlier we saw that if you put an asterisk in front of a formal
parameter in a method definition, multiple arguments in the call to the
method will be bundled up into an array. Well, the same thing works in
reverse.
When you call a method, you can explode an array, so that each of its
members is taken as a separate parameter. Do this by prefixing the
array argument (which must follow all the regular arguments) with an
asterisk.
def five(a, b, c, d, e)
|
"I was passed #{a} #{b} #{c} #{d} #{e}"
|
end
|
|
five(1, 2, 3, 4, 5 )
|
� |
"I was passed 1 2 3 4 5"
|
five(1, 2, 3, *['a', 'b'])
|
� |
"I was passed 1 2 3 a b"
|
five(*(10..14).to_a)
|
� |
"I was passed 10 11 12 13 14"
|
|
|