Reputation: 1
class Node
attr_accessor :value
def initialize(value)
@value
end
end
class Testo
attr_accessor :root
def method
@root = Node.new(4)
current = @root
current.value = 5
end
end
testing = Testo.new
testing.method
puts testing.root.value #=> Returns 5
I don't understand. Is the local variable current
now an instance variable? Is it a copy of @root
? Shouldn't root
be 4 instead of 5?
Upvotes: 0
Views: 72
Reputation: 1021
Variables in Ruby are references to objects.
What you are really doing is instantiating a new Node object with value=4
and then referencing to it with 'current' variable(name) (also @root
is referencing the same object) and then changing its value to 5.
Upvotes: 1