how do i run the function?

R

Rick Tan

If i want to run a function whose name is contained in a string, how do
I do that in Ruby? or is this not feasible?

For example

def a
puts "a"
end

def b
puts "b"
end

mymethod="a"

In the above code snippet, i like to run the function a by using the
string mymethod. What trick if any can i use in ruby to make this
happen?

Thanks.
 
C

Caius Durling

If i want to run a function whose name is contained in a string, how = do
I do that in Ruby? or is this not feasible?
=20
For example
=20
def a
puts "a"
end
=20
def b
puts "b"
end
=20
mymethod=3D"a"
=20
In the above code snippet, i like to run the function a by using the
string mymethod. What trick if any can i use in ruby to make this
happen?


Object#send. http://ruby-doc.org/core/classes/Object.html#M000332 (It'll =
take a string as well as a symbol.)

C
 

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,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top