From: ara.t.howard@... Date: 2006-03-12T05:09:42+09:00 Subject: Re: attribute setters for instance variables On Sun, 12 Mar 2006 eliben@gmail.com wrote: > Hello, > > The Pickaxe book presents the following example: > > class Incorrect > attr_accessor :one, :two > def initialize > one = 1 > self.two = 2 > end > end > > obj = Incorrect.new > > p obj.one > p obj.two > > The first thing printed is 'nil' and not '1' since "one = 1" assigns to > a local variable of method initialize instead of the instance variable. > So far it's clear. > Now, they propose a solution: to use self.one instead of just one. > Won't be using @one simpler and also correct ? What am I missing ? def one= arg log{ "somthing about one" } validate{ arg } notify_observers :one, arg @one = arg end just a few examples. this become even more true when the number attribues increases since you can class C ATTRIBUTES = %w[ one two three four five six seven ] def initialize kws = {} kws.each{|kw, val| send "#{ kw }=", val} end end in one line even if the number of attributes is huge. i'd also argue that the simplest thing is to simply make the getter take and optional argument so one can do def initialize one 1 two 2 end but that's a matter of taste. hth - cheers. -a -- share your knowledge. it's a way to achieve immortality. - h.h. the 14th dali lama