From: "Mauricio Fernández" Date: 2002-08-10T19:00:23+09:00 Subject: Re: ambiguity between local variable assignment and writter method On Sat, Aug 10, 2002 at 07:07:11AM +0900, Tom Sawyer wrote: > On Fri, 2002-08-09 at 15:37, Dave Thomas wrote: > > The behavior of parent classes would vary if they were subclassed by > > classes that defined specific accessors, which might be upsetting. > > could you elaborate on that a touch more? perhaps a small example? > This is how I see it: class A def initialize(blah) @blah = blah end def amethod(*args) # do things with args puts "I don't expect anything special to happen in the next line" @blah = ... # doesn't use accessor as there isn't one end end class B < A def blah=(thing) # do something with thing puts "Hey, doing things in self.blah=, which may be wrong!" @blah = ... end end obj = B.new obj.amethod(...) # vim: ts=4: @blah=... inside amethod, defined in class A, will use self.blah=, which could be wrong if you really need low-level access to the attribute. amethod expects @blah not to use an accessor, so you're changing the behavior of things belonging to the superclass. This isn't as if you were overloading a method, since there's no easy way to avoid self.blah= being called... One solution: each method using @attr=... would have to ignore accessors defined in derived classes. This involves changing the way Ruby looks for methods, but I don't think it would be that hard. This would however make Ruby dirtier, and I don't quite think I'd like that :-| -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com 'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux over the wire". Film at 11.' -- Linus Torvalds