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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could get in a lot of trouble for sending meth. On May 8, 4:12 pm, Philip Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> > 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Use a variable function ie $func = "calculate"; $result = $func(); On 5/9/07, LeonB <leon-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote:> > > 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? > > > > >-- http://www.web-buddha.co.uk --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks. But could I create the function "calculate" on the fly? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote:> > 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? > > > > >-- Best regards, Yuri Leikind --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m not sure if I follow but would object#send :thing work? On 5/9/07, LeonB <leon-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote:> > > 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? > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
One more possible solution: http://www.ruby-doc.org/core/classes/Module.html#M001677 On 5/9/07, Yuri Leikind <yuri.leikind-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote: > > > > 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? > > > > > > > > > > > > > -- > Best regards, > Yuri Leikind >-- Best regards, Yuri Leikind --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---