Invoking a method by name


#1

Hello all,

I have a need to invoke methods dynamically by name.

Say I have a method like:

def invoke_method_with_name(method_name)
person = Person.find(params[:id])
method_to_invoke = ~~ some code to find the method from a string
containing the method name ~~
person.method_to_invoke
end

I do this in Objective C with something like the following:

SEL setWidthHeight;
setWidthHeight = @selector(setWidth:height:);
or
setWidthHeight = NSSelectorFromString(aBuffer);

Where SEL is a special type representing an Objective C method
signature, and the @selector directive or
NSSelectorFromString(aBuffer) is used to find the method.

I have a feeling that I’m making this more complicated than need be.

Is it possible to do something as simple as:

person.#{method_to_invoke}


#2

I have a feeling that I’m making this more complicated than need be.

Is it possible to do something as simple as:

person.#{method_to_invoke}

You want send()…

http://www.ruby-doc.org/core/classes/Object.html#M000334


#3

Philip,

Excellent, that’s just what I was looking for.