Calling a method whose name I only know at run-time

C

Chad Thatcher

Hi folks,

I have a handful of methods that perform operations on chunks of data.
Which of these methods is called is specified in a YAML configuration so
I only know the method names at run-time, which are, of course, strings.
I am familiar with eval but would like to avoid using it to keep my code
safe. Is there another way I can do it?

Thanks,

Chad.
 
J

Jan Svitok

Oops, solved it. Sorry, I missed Object#method the first time around.
8)

And there is the Object#send that will do what you want, i.e.
any_object.send("method1", arg1, arg2)

or

any_object.send("method1", *args)

(if send insists on symbols for method names, use String#to_sym)
 
D

dblack

Hi --

And there is the Object#send that will do what you want, i.e.
any_object.send("method1", arg1, arg2)

or

any_object.send("method1", *args)

(if send insists on symbols for method names, use String#to_sym)

It doesn't; you can use either.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
C

Chad Thatcher

It looks like send is actually more appropriate for my needs, thanks,
I'll give it a go.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top