Hey,
Whenever I want to DRY some code, I want to do the following:
irb(main):001:0> @my_first_var = ''RoR rocks!''
=> "RoR rocks!"
irb(main):002:0> [''first''].each do |attr|
irb(main):003:1* puts @my_"#{attr}"_var
irb(main):004:1> end
Surprisingly, this perfectly works:
array.each do |sym|
define_method("#{sym}=") do
../..
end
end
Any idea for the first snippet to work?
Thanks in advance.
--
,========================.
| Pierre-Alexandre Meyer |
| email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org |
`========================''
--~--~---------~--~----~------------~-------~--~----~
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 -- On 3/6/07, Pierre-Alexandre Meyer <pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org> wrote:> > Hey, > > Whenever I want to DRY some code, I want to do the following: > > irb(main):001:0> @my_first_var = ''RoR rocks!'' > => "RoR rocks!" > irb(main):002:0> [''first''].each do |attr| > irb(main):003:1* puts @my_"#{attr}"_var > irb(main):004:1> endTry this: puts instance_variable_get("@my_#{attr}_var")> Surprisingly, this perfectly works: > > array.each do |sym| > define_method("#{sym}=") do > ../.. > end > endWhat''s surprising about it? :-) David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. 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 -~----------~----~----~----~------~----~------~--~---
On Tue, Mar 06, 2007 at 05:26:04PM -0500, David A. Black wrote :> Try this: > > puts instance_variable_get("@my_#{attr}_var")Dude, that''s pretty cool. Thanks.> What''s surprising about it? :-)That the first piece of code doesn''t work and the second does. There is no big difference between them, is it? -- ,========================. | Pierre-Alexandre Meyer | | email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org | `========================'' --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
For the posterity:
irb(main):001:0> attr = ''first''
=> "first"
irb(main):002:0>
instance_variable_set("@my_#{attr}_var",''Ror
rocks!'')
=> "Ror rocks!"
irb(main):003:0> instance_variable_get("@my_#{attr}_var")
=> "Ror rocks!"
irb(main):004:0>
instance_variable_set("@my_#{attr}_var",''Django
doesn\''t!'')
=> "Django doesn''t!"
irb(main):005:0> instance_variable_get("@my_#{attr}_var")
=> "Django doesn''t!"
--
,========================.
| Pierre-Alexandre Meyer |
| email : pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org |
`========================''
--~--~---------~--~----~------------~-------~--~----~
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 -- On 3/6/07, Pierre-Alexandre Meyer <pam-1sEOgp2Wo8Qdnm+yROfE0A@public.gmane.org> wrote:> > On Tue, Mar 06, 2007 at 05:26:04PM -0500, David A. Black wrote : > > Try this: > > > > puts instance_variable_get("@my_#{attr}_var") > > Dude, that''s pretty cool. Thanks. > > > What''s surprising about it? :-) > > That the first piece of code doesn''t work and the second does. There is > no big difference between them, is it?In the first one you did something like: puts @__"#{attr}"__ rather than putting it all in a string. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. 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 -~----------~----~----~----~------~----~------~--~---