E
eliben
Hello,
The Pickaxe book presents the following example:
class Incorrect
attr_accessor ne, :two
def initialize
one = 1
self.two = 2
end
end
obj = Incorrect.new
p obj.one
p obj.two
The first thing printed is 'nil' and not '1' since "one = 1" assigns to
a local variable of method initialize instead of the instance variable.
So far it's clear.
Now, they propose a solution: to use self.one instead of just one.
Won't be using @one simpler and also correct ? What am I missing ?
Thanks
The Pickaxe book presents the following example:
class Incorrect
attr_accessor ne, :two
def initialize
one = 1
self.two = 2
end
end
obj = Incorrect.new
p obj.one
p obj.two
The first thing printed is 'nil' and not '1' since "one = 1" assigns to
a local variable of method initialize instead of the instance variable.
So far it's clear.
Now, they propose a solution: to use self.one instead of just one.
Won't be using @one simpler and also correct ? What am I missing ?
Thanks