On Thu, Aug 16, 2012 at 3:51 PM, ajay paswan <lists / ruby-forum.com> wrote:
> suppose
> var="world"
> I want to call a method called hello_world() using the variable var. How
> to do that in ruby?
> and what feature of programing is this called?suppose
> var="world"
> I want to call a method called hello_world() using the variable var. How
> to do that in ruby?
> and what feature of programing is this called?
>
> to be clear:
> I want something like calling the method like :
> hello_+$var+()

You can do

send "hello_#{var}"

to dispatch to a method with a calculated name.  An alternative
approach is to do this:

my_methods = {
  'world' => lambda { puts "hello world" },
  'moon' => lambda { puts "hello moon" },
  'sun' => lambda { puts "hello sun" },
}

and then

my_methods[var].call

But in that case you'd rather use a method argument, of course:

def hello(x)
  puts "hello #{x}"
end

hello("world")

hello is a method (or rather function but Ruby does not have functions
which are not associated with a particular object).  x is the
argument.  "#{...}" is string interpolation.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/