Forum: Ruby on Rails Object evaluation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
28c315da59bc0bc0a544ed4a75ad26e9?d=identicon&s=25 Pierre-Alexandre Meyer (Guest)
on 2007-03-06 23:15
(Received via mailing list)
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@mouraf.org |
`========================'
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 David A. Black (Guest)
on 2007-03-06 23:26
(Received via mailing list)
Hi --

On 3/6/07, Pierre-Alexandre Meyer <pam@mouraf.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> 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

--
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)
28c315da59bc0bc0a544ed4a75ad26e9?d=identicon&s=25 Pierre-Alexandre Meyer (Guest)
on 2007-03-07 00:01
(Received via mailing list)
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@mouraf.org |
`========================'
28c315da59bc0bc0a544ed4a75ad26e9?d=identicon&s=25 Pierre-Alexandre Meyer (Guest)
on 2007-03-07 00:24
(Received via mailing list)
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@mouraf.org |
`========================'
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 David A. Black (Guest)
on 2007-09-25 23:04
(Received via mailing list)
HI --

On 3/6/07, Pierre-Alexandre Meyer <pam@mouraf.org> wrote:
> 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)
This topic is locked and can not be replied to.