Forum: Ruby on Rails Calling method through variable

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F63699bf9b8cbfe41966473ed2002985?d=identicon&s=25 Daniel Pérez (dperezrada)
on 2007-01-19 14:44
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...
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2007-01-19 15:18
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-01-19 15:24
(Received via mailing list)
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 topic is locked and can not be replied to.