Hi --

On Fri, 20 Sep 2002, Matthew, Graeme wrote:

> thanks for the help, i am willing to use the attr_reader, attr_writer
> options however the problem with these is that lets say you need to trigger
> of some events when an attribute is set, then how would you do it through
> attr_reader / writer ?

You wouldn't -- you'd have to roll your own.  For example:


   class A
     attr_reader :thing
     def thing=(val)
       puts "OK, I am now setting @thing to #{val}!"
       @thing = val
       puts "Now I can do other stuff, if needed...."
     end
   end

   a = A.new
   a.thing = "blah"

Note that I've used attr_reader to save the trouble of writing the
getter method:

  def thing
    @thing
  end

But I wrote the setter method by hand, because there was more going on
than attr_writer would have provided.

Another thing to keep in mind is that all of this is purely
conventional.  You can write methods with '=' in them that don't set
anything:

   class B
     def thing=(val)
       puts "Pointless use of '='!"
     end
   end

   b = B.new
   b.thing = "nothing"


David

-- 
David Alan Black                      | Register for RubyConf 2002!
home: dblack / candle.superlink.net     | November 1-3
work: blackdav / shu.edu                | Seattle, WA, USA
Web:  http://pirate.shu.edu/~blackdav | http://www.rubyconf.com