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.
LeonB (Guest)
on 2007-05-09 01: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?
Philip H. (Guest)
on 2007-05-09 02: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
dasil003 (Guest)
on 2007-05-09 05:06
(Received via mailing list)
You could get in a lot of trouble for sending meth.
LeonB (Guest)
on 2007-05-09 16: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?
Dave G. (Guest)
on 2007-05-09 16:49
(Received via mailing list)
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
LeonB (Guest)
on 2007-05-09 16:53
(Received via mailing list)
Thanks. But could I create the function "calculate" on the fly?
Yuri L. (Guest)
on 2007-05-09 16: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 <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.
Yuri L. (Guest)
on 2007-05-09 16:58
(Received via mailing list)
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.
Russell N. (Guest)
on 2007-05-09 16: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.