Hi --

On Sun, 4 Aug 2002, Harry Ohlsen wrote:

> I thought I might be able to maybe write my own versions of attr_writer and
> attr_accessor that created a special version of field=() that included the
> processing I want, but the following example shows that won't work.
>
>     class Fred
>        def a=(a)
>            $stderr.puts "Setting @a to #{a}"
>            @a = a
>        end
>
>        def initialize(a)
>            @a = a
>        end
>    end
>
>    f = Fred.new(123)
>
>    f.a = 456
>
> When you run this, it prints "Setting @a to 456".  The initial setting of @a
> in initialize doesn't get caught.  Ie, a=() doesn't seem to come into effect
> until AFTER the instance variable has been created.

It's not that -- it's that there's no call to #a= in your code.  @a=a
is just an assignment to a variable, and a=(a) (in the typical
attr_writer case) is a wrapper for @a=a, not the other way around.

(See Pit's suggestion about self.a = a, which calls the method a=().)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav