Using string variable to call a method

Hello all,

How can I use a string variable as part of a method name I’m calling?
example say I want to call the method say_hello using the following:

def say_hello
puts “Hello!”

str = “hello”


Thanks in advance!! – BTR

Alle lunedì 7 gennaio 2008, Bryan R. ha scritto:


Thanks in advance!! – BTR

send “say_#{str}”


You can use: eval, instance_eval, class_eval, module_eval oder send
The different evals are described here:
(click on the “N” to read the next message in the thread)
send is as far as I understand it, used to send messages to objects.
Keep in mind that method calls are messages send to objects.

In general, the send method is preferred over evalling a string when

In addition to send “say_#{str}”, you can do method(“say_#{str}”.to_sym)


Along these same lines, is it possible to do a require and include
dynamically, getting the name of the file to require and module to
from a string?

send :include, Module.const_get(ARGV[1]) worked. Thanks!!

require is obvious, the method takes a string already.

Including a module, I think you can do:

ClassName.send(:include, Module.const_get(“ModuleName”))

or if you’re working with an instance variable, replace :include with