From: Christoph Rippel Date: 2001-03-06T06:39:24+09:00 Subject: [ruby-talk:12063] Re: RCR: shortcut for instance variable initialization > From: dave@thomases.com [mailto:dave@thomases.com]On Behalf Of Dave > Thomas > Sent: Monday, March 05, 2001 01:12 PM > To: ruby-talk ML; ruby-talk@netlab.co.jp > Subject: [ruby-talk:12060] Re: RCR: shortcut for instance variable > initialization > > > "Christoph Rippel" writes: > > > class Whatever > > > > def will_be_rounded=(number) > > raise 'Not a number' unless kind_of? Numeric > > @will_be_number = number.round > > end > > > > def initialize(will_be_rounded@) > > ... > > end > > attr_accessor :will_be_rounded > > end > > > > does a call to initialize invoke ``@will_be_rounded='' > > or```will_be_rounded=''? > > There is no such method as '@will_be_rounded='. The attr_accessor Besides that, it should have been ``@will_be_rounded = number.round'' the question in my mind was if the line ``@will_be_rounded = number'' is created, or the function ``will_be_rounded=(number)'' is called (but you can always cook up a convention;-) > call will overwrite the method you defined with it's own: > > class Dave > def val=(n) > puts "in my val" > @val = 123 > end > attr_accessor :val > end > > d = Dave.new > d.val = 432 > p d.val #=> 432 > > > > Ps. I sometimes wonder if Ruby could have done without > > variables at all (I believe this) and only use attributes > > but this is a different matter. > > Attributes are simply accessor functions to instance variables. I know this, I was thinking that would have been forced into writing (basically reviving standard variable declaration I still prefer;-) def bla(right) attr_accessor :left left = right end Christoph [..]