#integer
puts "For number"
a=1
b=a
puts a.object_id
puts b.object_id
b=2
puts b.object_id
b=1
puts b.object_id
For number
3
3
5
3
#You can see that:
# 1) for the same integer, the object_id is the same
# 2) When b change, a will not change
#string
puts "For String"
a="Hello world!"
b=a
puts a.object_id
puts b.object_id
b="Bye world!"
puts b.object_id
b="Hello world!"
puts b.object_id
b="Why?"
puts b.object_id
b="Why?"
puts b.object_id
puts "Why?".object_id
For String
40660112
40660112
40660076
40660052
40660028
40660004
40659980
# 1) When you assign b=a, the object_id of b and a is same
# 2) but when you change b's string, the b's object_id will change
# 3) if you change b's string again, the b's object_id will also change
puts "For Array"
a=[1,2,3,4]
b=a
puts a.object_id
puts b.object_id
b[2]=100
puts b.object_id
puts b.join(",")
puts a.object_id
puts a.join(",")
For Array
21563588
21563588
21563588
1,2,100,4
21563588
1,2,100,4
# 1) when b=a, the b and a will be the same object_id
# 2) when you change b's elements, b's object_id will be same
# 3) when you change b's elements, a will also be changed
I think it's very interesting experiments. Hope someone can give us a clear
explanation.
···
--
Huazhi Gong (Hank)