If I have a string variable that contains the "name" of another variable. How can I access that other variable for both assignment and evaluation? e.g. x = 100 nameofvar = "x" I want to set the value of the variable specified by nameofvar and also get it''s value. Seems like some sort of reflection calls would be needed for this... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jul-23 12:48 UTC
Re: Evaluating a variable from it''s name
Hi -- On Mon, 23 Jul 2007, rlaferla wrote:> > If I have a string variable that contains the "name" of another > variable. How can I access that other variable for both assignment > and evaluation? > > e.g. > > x = 100 > nameofvar = "x" > > I want to set the value of the variable specified by nameofvar and > also get it''s value. > > Seems like some sort of reflection calls would be needed for this...There''s eval, but in almost seven years of Ruby program I have never needed to do that, and would be very surprised if it were really a good idea. Anyway, that wasn''t your question, so I''ll leave it at that :-) David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
jeroen.knoops-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-23 13:23 UTC
Re: Evaluating a variable from it''s name
Hello, See the following code: class Reflection attr_accessor :x def y "y" end def call_method (method_name) self.send(method_name) end end Now you can do the following things:>> r = Reflection.new=> #<Reflection:0xb7172b60>>> r.call_method("y")=> "y">> r.call_method("x")=> nil>> r.x = "test"=> "test">> r.call_method("x")=> "test">>Best regards, Jeroen On Jul 23, 2:24 pm, rlaferla <robertlafe...-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote:> If I have a string variable that contains the "name" of another > variable. How can I access that other variable for both assignment > and evaluation? > > e.g. > > x = 100 > nameofvar = "x" > > I want to set the value of the variable specified by nameofvar and > also get it''s value. > > Seems like some sort of reflection calls would be needed for this...--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---