Passing a method reference and then calling it

How do you pass a method reference and call it on a particular object?
I know I can pass it as a string and call eval, but I’m trying to
avoid that performance hit.

On Fri, Jun 26, 2009 at 2:03 PM, Martin H. [email protected] wrote:

How do you pass a method reference and call it on a particular object? I
know I can pass it as a string and call eval, but I’m trying to avoid that
performance hit.

You could use a proc/lambda, and pass it just like any other object:
foo = lambda do
puts “word.”
end

def test(bar)
bar.call
end

test(foo)

Alex

Martin H. wrote:

How do you pass a method reference and call it on a particular object? I
know I can pass it as a string and call eval, but I’m trying to avoid
that performance hit.

obj = “something”
method = :reverse
obj.send method # faster than eval

That works, but what if I want to store the method reference for later
use?

That is exactly what I was look for! Thank you very much.