Calling a Function From a String With the Function’s Name in Ruby

How can I do what they are talking about here, but in Ruby?

How would you do the function on an object? and how would you do a global function (see jetxee’s answer on the post mentioned)?

EXAMPLE CODE:

event_name = "load"

def load()
  puts "load() function was executed."
end

def row_changed()
  puts "row_changed() function was executed."
end 

#something here to see that event_name = "load" and run load()

UPDATE: How do you get to the global methods? or my global functions?

I tried this additional line

puts methods

and load and row_change where not listed.

Calling a function from a string with the function’s name in Python

What is the best way to go about calling a function given a string with the function’s name in a Python program. For example, let’s say that I have a module foo, and I have a string whose contents are

calling function name from called function in c++ [duplicate]

This question already has an answer here: How do I find the name of the calling function? 6 answers Is there anyway to get the calling function name from called function in c++ without modifyin

Calling Mongoexport from Ruby

Just like calling Mongo functions in Ruby how to use Mongoexport from the Ruby. Right now I am doing mongoexport via system call system(/mongodb/bin/mongoexport -d surya -c users -q /#{query}/ -o

Calling a function from a string in Python throws error

I am trying to call a function from a string in Python as explained in Calling a function from a string with the function’s name in Python. Unfortunately, this doesn’t work, and the Python interpreter

Get Calling function name from Called function [duplicate]

Possible Duplicate: How can I find the method that called the current method? How can I get the calling function name from the called function in c#?

Mako calling function from string?

Is there an easy way to call a function given a string name in mako?

Calling Lua function from string_t

I have some functions declared and initialized in .lua file. Then, when I receive signal, I read string_t variable with the name of function to call from file. The problem is that I don’t know how to

Calling a Function From a String With the Function’s Name in C++

How can I call a C++ function from a string? Instead of doing this, call the method straight from string: void callfunction(const char* callthis, int []params) { if (callthis == callA { callA(); } e

Calling functions from within a function [duplicate]

This question already has an answer here: “call to undefined function” error when calling class method 3 answers I’m new to MVC patterns and OOP in general. I have an application in CodeIgniter

Calling nested function when function name is passed as a string

I’m trying to access a nested function by passing the function name in as a string and then calling it. Eg, see this post function outer(action){ window[outer][action](); function inner(){ alert(he

Answers

Personally I would setup a hash to function references and then use the string as an index to the hash. You then call the function reference with it’s parameters. This has the advantage of not allowing the wrong string to call something you don’t want to call. The other way is to basically eval the string. Do not do this.

PS don’t be lazy and actually type out your whole question, instead of linking to something.

To call functions directly on an object

a = [2, 2, 3]
a.send("length")

which returns 3 as expected

or for a module function

FileUtils.send('pwd')

and a locally defined method

def load()
    puts "load() function was executed."
end

send('load')

Use this:

> a = "my_string"
> meth = a.method("size")
> meth.call() # call the size method
=> 9

Simple, right?

As for the global, I think the Ruby way would be to search it using the methods method.

I have covered several ways for Ruby’s dynamic method calling in the past. Have a look at them.