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.
Hi --
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
Try this:
puts instance_variable_get("@my_#{attr}_var")
Surprisingly, this perfectly works:
array.each do |sym|
define_method("#{sym}=") do
../..
end
end
What's surprising about it? 
David
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?
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!"