Module level variable shadowing
Jacob Carlborg via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jun 29 11:24:01 PDT 2014
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
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list