Reputation: 2380
class R
def initialize(number)
@number = number
end
attr_accessor :number
end
r = R.new(3)
r.number => 3
r.@number => syntax error
r.(@number) => undefined method call
Why can't the instance variable invoked this way?
As far as I know thanks to the attr_accessor
def number
@number
end
So
r.number
method should return self.@number
which is r.@number
What did I miss?
Upvotes: 0
Views: 37
Reputation: 168071
r.number
method should returnself.@number
which isr.@number
No. Nowhere in the definition of the number
method says self.@number
. It says: @number
. It should return the value of @number
.
@number
is an instance variable, not a method. You cannot call it (like that or in any other way), you can only refer to it from an appropriate scope.
Upvotes: 4