From: Logan Capaldo Date: 2007-05-27T23:44:23+09:00 Subject: Re: local variables vs. methods On Sat, May 26, 2007 at 10:35:03PM +0900, Henrik Schmidt wrote: > As seen above, there are several ways of getting around this, but this > is the question: Why is this behaviour useful? As I see it, it's bad > practice to give a method and a local variable the same name. At least I > can't think of an example where it would make sense. Why not simply > disallow this or at least have the interpreter issue a warning? > Just to throw out another reason, that I don't think has been mentioned yet, it would be expensive. For the interpreter to issue a warning about local variables shadowing method names, it would require the interpreter to do a method lookup on _every_ variable assignment. And it couldn't even be at parse time, it would have to be every time. Eg: class A def bar foo = 1 # the interpreter doesn't yet know that there will be a foo # method foo end def foo "foo" end end # Therefore, it has to do at least 2 method lookups everytime I call # bar below, one for bar itself, and one to make sure foo isn't a method v = A.new v.bar v.bar v.bar It also can't cache the need to warn, because I can remove_method :foo, making the warning incorrect. Method lookup on every assign would not be a cheap operation. (I know we don't usually like to talk about speed here, but this would be _really_ bad). > Best regards, > Henrik Schmidt