I know this can be done.. rails creates methods from variables all the
time... but how do you code it?
say you have two strings.
a="test"
test="this is a test"
how do you get the contents of test with the variable a?
#{a}
well that doesn;t work!! I know this is a easy question for the ruby
gods out there.. but for a newbie learning the language it''s
hard.. :> HELP!!!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
> say you have two strings. > > a="test" > test="this is a test" > > how do you get the contents of test with the variable a? >Go read "The Ruby Way" class A attr_accessor :variable, :test end a = A.new a.variable = :test a.send a.variable if a.responds_to? a.variable send is a method in class Object that takes a symbol and an argument list. the symbol is used to call a method of a corresponding name which is given the arguments. Rails tends to use this in combination with method missing in active record for its handling of database attributes which I assume is what your referring to. -K -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
a = "test" test = "this is a test" eval a # => "this is a test" On Jan 17, 2008, at 11:12 AM, spokra wrote:> > I know this can be done.. rails creates methods from variables all the > time... but how do you code it? > > > say you have two strings. > > a="test" > test="this is a test" > > how do you get the contents of test with the variable a? > > #{a} > > well that doesn;t work!! I know this is a easy question for the ruby > gods out there.. but for a newbie learning the language it''s > hard.. :> HELP!!! > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Or depending on what you''re doing, why not do this:
a = "This is my #{test} variable."
test = "super cool"
-Ryan Kopf
On Jan 29, 10:12 am, Michael Bleigh
<mble...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> a = "test"
> test = "this is a test"
> eval a # => "this is a test"
>
> On Jan 17, 2008, at 11:12 AM, spokra wrote:
>
>
>
>
>
> > I know this can be done.. rails creates methods from variables all the
> > time... but how do you code it?
>
> > say you have two strings.
>
> > a="test"
> > test="this is a test"
>
> > how do you get the contents of test with the variable a?
>
> > #{a}
>
> > well that doesn;t work!! I know this is a easy question for the ruby
> > gods out there.. but for a newbie learning the language it''s
> > hard.. :> HELP!!!- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---