On Mon, Jun 29, 2009 at 2:44 PM, Wijnand Wiersma <wijnand / videre.net> 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
>
>