Calling method through variable


#1

I have a variable and I want to call a method with the name of the
variable
example:
method_variable = “sum”

def sum

end

How can i do that?

thank…


#2

Daniel Pérez wrote:

I have a variable and I want to call a method with the name of the
variable
example:
method_variable = “sum”

def sum

end

How can i do that?

thank…

self.send(method_variable)

Alan


#3

use the Object.send instance method

class Klass
def sum(a, b)
a + b
end
end

k = Klass.new
m = “sum”

k.send m.to_sym, 1, 2
=> 3

Chris