From: Clemens Hintze Date: 2000-03-31T23:57:39+02:00 Subject: [ruby-talk:02315] Question about attribute writers Dave Thomas writes: ... > The reason is that the assignment > > age = val/12 > > in Fred#ageInMonths= is creating a local variable 'age', rather than > invoking the age= method. > > This seems inconsistent with the fact that > > age / 12 > > in Fred#ageInMonths is correctly using the attribute reading > function. > > > Is this the expected behavior? At least I would expect it! It is like answered in your FAQ! How does Ruby determine what a variable is? It is really stright-forward, IMHO. Look if there was an assignment before yes => compile a variable access no => compile a method call. That means, the compiler seems to prefer to compile variable access over compiling method calls. So age / 12 would let the interpreter look whether if has already seen a assignment to a local variable. As it didn't it compile a method call! So Fred#age would be called. However age = val / 12 looks like an assignment. That means variable access! As Ruby prefer this, why should the compiler try to compile method call here? But, however, if it was written as self.age = val / 12 then Ruby is not able to think of an variable access here, as this syntax is invalid at that point! So it will compile a method call to Fred#age= Sorry for my confusing explanation. But I hope you got my point anyway. > > > Dave > \cle -- Clemens Hintze mailto: c.hintze@gmx.net