From: "Jesús Gabriel y Galán" Date: 2008-11-25T17:01:42+09:00 Subject: Re: my attr_accessor On Mon, Nov 24, 2008 at 11:17 PM, Shot (Piotr Szotkowski) wrote: > cibercitizen1: > >> I tried to > >> class Foo >> attr_accessor :bar1, :bar2 >> >> def initialize( &bloc ) >> instance_eval( &bloc ) >> end >> end > >> and then > >> f = Foo.new { >> bar1 = 12345 >> bar2 = 44444 >> } > >> but this doesn't work > Right, unless you precede it with self: > > f = Foo.new { > self.bar1 = 12345 > self.bar2 = 44444 > } > Another idiom is to yield self to the block like so: irb(main):001:0> class Foo irb(main):002:1> attr_accessor :bar1, :bar2 irb(main):003:1> def initialize irb(main):004:2> yield self irb(main):005:2> end irb(main):006:1> end => nil irb(main):007:0> f = Foo.new {|foo| foo.bar1 = "test"; foo.bar2 = "test2"} => # Jesus.