Module level variable shadowing
Ary Borenszweig via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jun 29 12:05:05 PDT 2014
On 6/29/14, 3:24 PM, Jacob Carlborg wrote:
> On 2014-06-28 14:20, Ary Borenszweig wrote:
>
>> In Ruby the usage of a variable is always prefixed: `@foo` for instance
>> vars, `$foo` for global variable, `FOO` for constant. You can't make a
>> mistake. It's... perfect :-)
>
> Oh, that's where you're wrong, very wrong :). Take this for example:
>
> class Foo
> attr_accessor :bar
>
> def initialize
> @bar = 3
> end
>
> def foo
> puts bar # prints "bar", the instance variable, via a getter
> bar = 4
> puts bar # prints "bar", the local variable
> puts self.bar # prints "bar", the instance variable, via a getter
> end
> end
>
> Foo.new.foo
>
That's the only "confusing" thing about Ruby. But I never had troubles
with it. In fact, when I use a local variable I never want to call a
method with that same name. And when I do, I put a parenthesis. It's a
pretty simple rule to learn.
A simple solution would be to force parenthesis on method calls that
don't have a receiver. So "bar" would always be a variable, "bar()"
would be a method call and "foo.bar" and "foo.bar()" would also be
method calls.
More information about the Digitalmars-d
mailing list