[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Sending Multiple Args to a Method Object

Phillip Hutchings

8/1/2006 11:31:00 PM

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)
--
Phillip Hutchings
http://www.sit...

2 Answers

Justin Collins

8/1/2006 11:36:00 PM

0

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


Justin Collins

8/1/2006 11:51:00 PM

0

Justin Collins wrote:
> 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).
>
Or maybe he wasn't. Either way.

-Justin