Something like $$ in php


#1

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?


#2

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


#3

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


#4

Use a variable function ie

$func = “calculate”;

$result = $func();

On 5/9/07, LeonB removed_email_address@domain.invalid wrote:

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?


http://www.web-buddha.co.uk


#5

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


#6

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?


#7

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

On 5/9/07, LeonB removed_email_address@domain.invalid wrote:

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?


Best regards,
Yuri L.


#8

One more possible solution:

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

On 5/9/07, Yuri L. removed_email_address@domain.invalid wrote:

}

Thanks for the replies.

Yuri L.


Best regards,
Yuri L.


#9

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