# A interesting obervation about object\_id method

**URL:** <https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814>\
**Category:** Ruby\
**Created:** [December 21, 2005, 12:42pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814 "2005-12-21T12:42:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hank\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@Hank\_G](https://www.ruby-forum.com/u/Hank_G)\
**Post date:** [December 21, 2005, 12:42pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814/1 "2005-12-21T12:42:12Z")

</div>

#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.

---

<div class="post-metadata">

**Author:** ![Hank\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@Hank\_G](https://www.ruby-forum.com/u/Hank_G)\
**Post date:** [December 21, 2005, 2:03pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814/2 "2005-12-21T14:03:33Z")

</div>

Anyone else notice that the object\_id of an integer (at least for  
Fixnum) is twice the integer plus 1?

irb(main):030:0\> 1.object\_id  
=\> 3  
irb(main):031:0\> 2.object\_id  
=\> 5  
irb(main):032:0\> 6534.object\_id  
=\> 13069  
irb(main):033:0\> 2\*6534+1  
=\> 13069

---

<div class="post-metadata">

**Author:** ![Hank\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@Hank\_G](https://www.ruby-forum.com/u/Hank_G)\
**Post date:** [December 21, 2005, 12:48pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814/3 "2005-12-21T12:48:14Z")

</div>

puts “For String”  
a=“Hello world!”  
b=a  
puts a.object\_id  
puts b.object\_id  
b=“Bye world!”  
puts b.object\_id

The object id of b is changing bcz u are redifining b using “=”  
operator. Instead if u use b.replace(“Bye word!”) thn the object id  
will not change.

Hank G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: #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)

---

<div class="post-metadata">

**Author:** ![Hank\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@Hank\_G](https://www.ruby-forum.com/u/Hank_G)\
**Post date:** [December 21, 2005, 3:12pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814/4 "2005-12-21T15:12:46Z")

</div>

Hank G. wrote:

> #integer
> 
> #You can see that:
> 
> # 1) for the same integer, the object\_id is the same

That’s because Fixnums are “immediate.” So are Symbols. It’s a  
performance thing. It also means, for example, you can’t define  
singleton methods on a Fixnum. Notice, actually, that you can decode the  
integer value _from_ the object-id, and vice versa.

> # 2) When b change, a will not change

In Ruby, variables are _references_ (a.k.a. pointers). You’re just  
telling b to reference a different object.

> #string
> 
> # 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

Strings, on the other hand, are not immediate. “Hello World”.object\_id  
!= “Hello World”.object\_id – each literal reference creates a new  
String object. The #replace method Chintan mentioned, however, gets  
around this, by wrapping the existing String object around a new string  
– and since a and b were originally pointing to the same object, a  
would change with it.

> > >
> 
> # 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

a and b are merely references to that one Array object, stored out in  
voodoo land. The Array, itself, contains four references to other  
objects. In this case, they were all Fixnums, and hence immediate (and  
immutable), but try this out for size:

str = “my dog”  
a=[1,2,3,4,str]  
b=[:alpha, :omega, str]  
str[‘dog’] = ‘cat’  
p a #=\> [1, 2, 3, 4, “my cat”]  
p b #=\> [:alpha, :omega, “my cat”]

Both Arrays, while unique, reference the same String object, and so both  
change at once. If, however, you did:

str = “my dog”  
a=[1,2,3,4,str]  
b=[:alpha, :omega, str]  
str = ‘my cat’  
p a #=\> [1, 2, 3, 4, “my dog”]  
p b #=\> [:alpha, :omega, “my dog”]

All you did was create a new String, “my cat”, and point str to it. The  
Arrays still point to the original “my dog” String.

> I think it’s very interesting experiments. Hope someone can give us a clear  
> explanation.

I hope that helped.

Devin

---

<div class="post-metadata">

**Author:** ![Hank\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@Hank\_G](https://www.ruby-forum.com/u/Hank_G)\
**Post date:** [December 21, 2005, 10:32pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814/5 "2005-12-21T22:32:39Z")

</div>

On Dec 21, 2005, at 5:00 AM, Dan D. wrote:

> =\> 13069  
> Using pointers to odd memory addresses a common way of representing  
> integers.

non-Fixnums always have even ids since they are pointers to valid  
addresses.

nil, true and false have special values.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Hank\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@Hank\_G](https://www.ruby-forum.com/u/Hank_G)\
**Post date:** [December 21, 2005, 11:39pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814/6 "2005-12-21T23:39:57Z")

</div>

Devin M. wrote:

> Hank G. wrote:
> 
> > #integer
> > 
> > #You can see that:
> > 
> > # 1) for the same integer, the object\_id is the same
> 
> That’s because Fixnums are “immediate.” So are Symbols. It’s a  
> performance thing. It also means, for example, you can’t define  
> singleton methods on a Fixnum. Notice, actually, that you can decode the  
> integer value _from_ the object-id, and vice versa.

Even without going to implementation details, the code  
is logical: each number is a constant instance of the  
Fixnum (or other) class:

# Pseudo

1 = Fixnum.new(“Number one”)

Then, logically, all variables referencing the number  
1 are pointing to the same object.

> Devin

E

---

<div class="post-metadata">

**Author:** ![Hank\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@Hank\_G](https://www.ruby-forum.com/u/Hank_G)\
**Post date:** [December 21, 2005, 1:18pm UTC](https://www.ruby-forum.com/t/a-interesting-obervation-about-object-id-method/50814/7 "2005-12-21T13:18:23Z")

</div>

On Wed, 2005-12-21 at 20:40 +0900, Hank G. wrote:  
[SNIP]

> I think it’s very interesting experiments. Hope someone can give us a clear  
> explanation.  
> A string is mutable, an integer is inmutable, that’s why IMHO.  
> Try the same with symbols(:hello instead of “hello”). These are  
> inmutable.
