Hi all. I have this piece of code: Parked at Loopia. 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: Parked at Loopia. 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: Parked at Loopia. 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.
Use a variable function ie
$func = “calculate”;
$result = $func();
On 5/9/07, LeonB [email protected] 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?
Thanks. But could I create the function “calculate” on the fly?
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?
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 [email protected] 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.
One more possible solution:
http://www.ruby-doc.org/core/classes/Module.html#M001677
On 5/9/07, Yuri L. [email protected] wrote:
}
Thanks for the replies.
Yuri L.
–
Best regards,
Yuri L.
I’m not sure if I follow but would object#send :thing work?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs