From: David Alan Black Date: 2001-10-27T09:40:46+09:00 Subject: [ruby-talk:23574] Re: Fast reply needed: class var trouble Hello -- On Sat, 27 Oct 2001, HarryO wrote: > In article > , "Robert > Feldt" wrote: > > > The Real PoRLS (Principle of Roberts Least Surpris) here would be for > > Ruby to conclude that it cant be a local since it would be useless to > > declare a local when value never used. Something for rite matz? ;-) > > Maybe I'm missing something here, but I can't think of a case where I'd > want a local variable to override an accessor method like this. I > actually think that violates the POLS, since I wouldn't expect it. > > In other words, I would think the parser should always interpret that as > being a call to the accessor method, in which case there would be no > ambiguity. But that assumes that Ruby has enough information to know this, at the time it's deciding what the "x" in "x = expr" is. Given Ruby's dynamicness, however, there's no way to know whether the method self.x= exists or not. For (not very elegant) example: class Thing def initialize instance_eval "def x=(n); @x = n; end" end def meth x = 100 # ... end end or: class Other def stuff y = 200 # ... end end class Other def y=(n) @y = n end end And if you get into trying to distinguish between cases where the method has already been defined vs. those where it hasn't, then things will have the potential to change in nasty ways when you reorganize or extend your code. (For example, you might add an accessor method that uses the same name as a local variable somewhere in your program; any assignment to that local variable would then turn into a method call!) I think a better way to look at it is that leaving off "self." is a nice way to unclutter code, when it's possible to do it, but that it isn't always possible to do it :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav