Forum: Ruby Re: Newbie question: variable method call

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4a9561ad5b2b9dc60c76993c9bed5b5e?d=identicon&s=25 Matthieu TC (Guest)
on 2007-05-26 08:06
(Received via mailing list)
Assuming your variable contains a string, try this:

  method_name = "feed"


----- Original Message ----
From: Kal Starkis <>
To: ruby-talk ML <>
Sent: Saturday, May 26, 2007 3:50:41 PM
Subject: Newbie question: variable method call

Is there a way to use the value of a variable to call a method of that

For example, rather than use the following code to determine which
method of the 'pet' object to call:

if action == "feed"
elsif action == "walk"
elsif action == "pat"

I'd rather just do something like this:


The problem is, Ruby thinks I am trying to call a method named 'action',
instead of evaluating the variable and using its value (e.g. 'feed').
This topic is locked and can not be replied to.