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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
84ff5f3d1f5b912a04d2acf63901501b?d=identicon&s=25 Chad Thatcher (builderchad)
on 2007-02-03 11:52
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.
84ff5f3d1f5b912a04d2acf63901501b?d=identicon&s=25 Chad Thatcher (builderchad)
on 2007-02-03 12:04
Oops, solved it.  Sorry, I missed Object#method the first time around.
8)
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jan Svitok (Guest)
on 2007-02-03 12:24
(Received via mailing list)
On 2/3/07, Chad Thatcher <chad@zulu.net> wrote:
> 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)
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-02-03 14:13
(Received via mailing list)
Hi --

On Sat, 3 Feb 2007, Jan Svitok wrote:

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

It doesn't; you can use either.


David
84ff5f3d1f5b912a04d2acf63901501b?d=identicon&s=25 Chad Thatcher (builderchad)
on 2007-02-03 19:23
It looks like send is actually more appropriate for my needs, thanks,
I'll give it a go.
This topic is locked and can not be replied to.