Something like $$ in php

Hi all. I have this piece of code: http://pastie.caboo.se/59971. How
can I use the string in 'name' to use as a method of declaration?

$methodname = 'new'
Just like using $obj->$methodname in php?

Hi all. I have this piece of code: http://pastie.caboo.se/59971. How
can I use the string in 'name' to use as a method of declaration?

obj = MyObject.new
meth = "my_method"
obj.send(meth) # same as obj.my_method

-philip

You could get in a lot of trouble for sending meth.

Thanks for the replies.

What I also want, is to create a function with a variable name.

So if meth = 'thing'

I want to create a method like

thing_some_method()

Is there a simple way to do that?

I thought about using alias_method. Would that be a good idea? Or can
it be done simpeler?

Use a variable function ie

$func = “calculate”;

$result = $func();

Thanks. But could I create the function "calculate" on the fly?

This might be not what you want, but how about using Proc instances
stored in a hash:

procs[:some_action1] = lambda{ |a,b|
    a + b
}

procs[:some_action2] = lambda{ |a,b|
    a - b
}

puts procs[:some_action1].call(2,5)
puts procs[:some_action2].call(2,5)

Cheers,
Yuri

I’m not sure if I follow but would object#send :thing work?

One more possible solution:

http://www.ruby-doc.org/core/classes/Module.html#M001677