Passing a method reference and then calling it

M

Martin Hess

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.
 
A

Alex

[Note: parts of this message were removed to make it a legal post.]

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
 
J

Joel VanderWerf

Martin said:
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
 

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
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top