From: Clemens Hintze Date: 2000-04-01T01:24:25+02:00 Subject: [ruby-talk:02318] Re: Question about attribute writers Dave Thomas writes: > Clemens Hintze writes: > > > 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. > > True - I asked the wrong question. Rather than asking "is this the > expected behavior", I guess I meant "is this the behavior that someone > would expect?" It seems to violate the principle of least surprise. Hmm difficult thingy here, I think. I would never assume that age = 12; could invoke a method, because I *know* that this syntax normally means assignment to a local variable. It would be very strange to me to use a method 'age=', if I can access @age directly. Invoking a method would furthermore be more costly. I admit this it-could-be-a-variable-or-a-method-call thingy is one of the things, that disturbes me also sometimes in Ruby. > > Perhaps a warning would a good idea in these circumstances: > > warning: local variable eclipses instance method Erhm ... IMHO this warning is wrong! Because there is *no*syntax* that allow 'age = 12' meaning a method call to 'age='. So if this is invalid syntax concerning the Ruby grammar, how could we issue a warning that this method call would be globbered? We *are*not* able to call the method like this. And if we use self.age = 12 as intended, all ambiguities are removed! I would propose the opposite, if we really want to issue an warning. The warning should be issued for the case age / 12 where I want to divide the value of a variable 'age'. But because this variable does not exists, I will get a method call compiled. Here the compiler could issue an warning like: warning: ambigues variable access or method call I could by-pass this warning then by explicitely writing: age() / 12 But I admit, that this solution would probably issue too much warnings because the 'age / 12' syntax is often used! Perhaps it would be better to not change anything, and describe this case very carefully in the books and docs. Every language has its gotchas. We cannot eliminate all of them. This is one of Ruby. > > Dave \cle -- Clemens Hintze mailto: c.hintze@gmx.net