Extracting array values in Object.send.

A

Aaron Smith

How can I exract array values when using Object.send. See the example
below.

class TestSend

def test( arg1, arg2, arg3)
puts arg1
puts arg2
puts arg3
end

end

t = TestSend.new
t.send('test', 'hey', 1, 'rrr')

args = ['hey',1,'rrr']
t.send('test', args) #this throws an argument error, 3 for 1..

Thanks.
 
S

Suraj Kurapati

Aaron said:
How can I exract array values when using Object.send. See the example
below.

class TestSend

def test( arg1, arg2, arg3)
puts arg1
puts arg2
puts arg3
end

end

t = TestSend.new
t.send('test', 'hey', 1, 'rrr')

args = ['hey',1,'rrr']
t.send('test', args) #this throws an argument error, 3 for 1..

Use the splat operator to expand the array into individual method
arguments:

t.send:)test, *args)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top