From: Henrik Schmidt Date: 2007-05-27T06:25:04+09:00 Subject: Re: local variables vs. methods Trans wrote: > > On May 26, 9:35 am, Henrik Schmidt wrote: >> Hi there, >> >> I've been playing around with Ruby for a while, but there's still one >> particular feature of the language that doesn't make sense to me. If you >> write a class containing a method and a class with the same name, the >> interpreter will pick the variable over the method, unless you >> specifically tells it not to. For example, >> >> class Foo >> >> def output >> puts foo # "foo" >> foo = 42 >> puts foo # 42 >> puts self.foo # "foo" >> puts foo() # "foo" >> end >> >> def foo >> "foo" >> end >> >> end >> >> Foo.new.output >> >> 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? > > Perhaps we could ask about this from the opposite perspective. Would > it not be useful to define local methods --in method scope? > > def foo > def bar; 10; end > bar > end > > In effect then, a method is nothing more than a lazy variable having > it's own specific rules of scope. There's really no good reason to > restrict name clash. > > T. > > I didn't know you could do that. That's kinda horrible :)