J
Jian Lin
For this program:
class Point
def initialize(x,y)
@x,@y = x,y
@something = 123.456
end
def instance_variables_and_values
hash = {}
instance_variables.each {|n| hash[n] = instance_variable_get(n)}
return hash
end
end
p1 = Point.new(1,2)
p p1.instance_variables_and_values
p2 = Point.new(3,4)
p p1.instance_variables_and_values
p p2.instance_variables_and_values
so the hash is actually allocated in the Heap, and the local variable in
the Stack is just the "hash" reference?
So when the method returns, the "hash" variable, which is just a
reference to the hash object, is destroyed. But the hash object that is
in the Heap will stay as long as there is a reference to it (a reference
from outside)?
class Point
def initialize(x,y)
@x,@y = x,y
@something = 123.456
end
def instance_variables_and_values
hash = {}
instance_variables.each {|n| hash[n] = instance_variable_get(n)}
return hash
end
end
p1 = Point.new(1,2)
p p1.instance_variables_and_values
p2 = Point.new(3,4)
p p1.instance_variables_and_values
p p2.instance_variables_and_values
so the hash is actually allocated in the Heap, and the local variable in
the Stack is just the "hash" reference?
So when the method returns, the "hash" variable, which is just a
reference to the hash object, is destroyed. But the hash object that is
in the Heap will stay as long as there is a reference to it (a reference
from outside)?