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…
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…
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
…
endHow can i do that?
thank…
self.send(method_variable)
Alan
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs