Below is the test case, I was just trying to do something with dup
method. But I realized this weird behavior. I couldn't find any reasonable explanation.
class ObjectIdTest
attr_accessor :x, :y
def initialize
@x, @y = 1, 2
end
def object_ids
"x:#{@x.object_id}, y: #{@y.object_id}"
end
end
class ObjectIdTestChild < ObjectIdTest
attr_accessor :z
def initialize
@z = 3
end
def object_ids
super + " z: #{@z.object_id}"
end
end
oid1 = ObjectIdTest.new
oid2 = ObjectIdTestChild.new
p oid2.object_ids
oid3 = oid2.dup
p oid3.object_ids
Output: "x:4, y: 4 z: 7"
"x:4, y: 4 z: 7"
- Normally object ids' are longer (like 70322728590900), why these are so smaller? (Are they relative or something like that?)
- Why x's and y's object id are the same?
- Why oid2 and oid3 's instance variables have the same object id?