Forum: Ruby on Rails Something like $$ in php

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-05-08 23:52
(Received via mailing list)
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?
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-05-09 00:02
(Received via mailing list)
> 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
6e9db38e16957cc51cf9cee9de399249?d=identicon&s=25 dasil003 (Guest)
on 2007-05-09 03:06
(Received via mailing list)
You could get in a lot of trouble for sending meth.
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-05-09 14:45
(Received via mailing list)
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?
8125afaa6ade422d5d733d7a05222935?d=identicon&s=25 Dave Goodchild (Guest)
on 2007-05-09 14:49
(Received via mailing list)
Use a variable function ie

$func = "calculate";

$result = $func();

On 5/9/07, LeonB <leon@tim-online.nl> 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
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-05-09 14:53
(Received via mailing list)
Thanks. But could I create the function "calculate" on the fly?
8d1e089fd6a6703f5f13dbee271aa77c?d=identicon&s=25 Yuri Leikind (Guest)
on 2007-05-09 14:54
(Received via mailing list)
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 <leon@tim-online.nl> 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 Leikind
8d1e089fd6a6703f5f13dbee271aa77c?d=identicon&s=25 Yuri Leikind (Guest)
on 2007-05-09 14:58
(Received via mailing list)
One more possible solution:

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

On 5/9/07, Yuri Leikind <yuri.leikind@gmail.com> wrote:
> }
> > Thanks for the replies.
> >
> Yuri Leikind
>


--
Best regards,
Yuri Leikind
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 Russell Norris (Guest)
on 2007-05-09 14:58
(Received via mailing list)
I'm not sure if I follow but would object#send :thing work?
This topic is locked and can not be replied to.