self.__send__(:test_method) passing parameter to this

S

Sijo Kg

Hi
How can I pass parameters to the method, test_method And also would
like to know what is __ in __send__ ?

Thanks
 
B

Brian Candler

Sijo said:
Hi
How can I pass parameters to the method

foo.__send__:)test_method, arg1, arg2)

Or if the args are in an array already:

foo.__send__:)test_method, *args)
And also would
like to know what is __ in __send__ ?

It's two underscores :)

Here's what "ri send" says:

-------------------------------------------------------- Object#__send__
obj.send(symbol [, args...]) => obj
obj.__send__(symbol [, args...]) => obj
------------------------------------------------------------------------
Invokes the method identified by _symbol_, passing it any arguments
specified. You can use +__send__+ if the name +send+ clashes with
an existing method in _obj_.

class Klass
def hello(*args)
"Hello " + args.join(' ')
end
end
k = Klass.new
k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
 

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

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top