From: Mike Sassak Date: 2009-06-30T04:37:55+09:00 Subject: Re: attr_writers within blocks --0016368e1e76239f58046d81c1c8 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Mon, Jun 29, 2009 at 2:44 PM, Wijnand Wiersma wrote: > Hi all, > > It seems writer methods don't work within blocks, the names are resolved to > new local variable names I guess. > It is funny however the reader method does seem to work fine. > Is it possible to make it work anyway without changing the syntax within > the block? > > My example gist: http://gist.github.com/137741 > You can override the getter method for a DSL-ish syntax: class Foo attr_accessor :bar def initialize(&block) instance_eval(&block) end def bar(v=nil) v ? @bar = v : @bar end end f = Foo.new { bar 2 } f.bar # => 2 f.bar 3 f.bar # => 3 f.bar = 4 f.bar # => 4 HTH Mike > Kind regards, > Wijnand > > --0016368e1e76239f58046d81c1c8--