Justin Collins
8/1/2006 11:36:00 PM
Phillip Hutchings wrote:
> On 8/2/06, Harris Reynolds <hreynolds2@yahoo.com> wrote:
>> The ruby interpreter thinks that my single array is the parameter I
>> am passing in; however, the array I am passing in contains 4 object
>> that map directly to the 4 arguments the interpreter is looking for.
>> Is there another way to accomplish this other than using
>> method.call? Or am I using method.call incorrectly?
>
> Put a * before the array argument, it'll make Ruby expand it to fit
> the arguments, eg:
>
> def test(one, two, three, four)
> puts "#{one} #{two} #{three} #{four}"
> end
>
> args = [1, 2, 3, 4]
> m = method(:test)
> m.call(*args)
Note that you do not need to use Method#call to do this, it works for
all methods (since the OP was asking if he had to use it).
The above could have been (just for clarification):
def test(one, two, three, four)
puts "#{one} #{two} #{three} #{four}"
end
test(*[1, 2, 3, 4])
or
args = [1,2,3,4]
test(*args)
-Justin