A interesting obervation about object_id method

H

Hank Gong

------=_Part_17665_17982672.1135165188394
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

#integer
puts "For number"
a=3D1
b=3Da
puts a.object_id
puts b.object_id
b=3D2
puts b.object_id
b=3D1
puts b.object_id

#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=3D"Hello world!"
b=3Da
puts a.object_id
puts b.object_id
b=3D"Bye world!"
puts b.object_id
b=3D"Hello world!"
puts b.object_id
b=3D"Why?"
puts b.object_id
b=3D"Why?"
puts b.object_id
puts "Why?".object_id

# 1) When you assign b=3Da, 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=3D[1,2,3,4]
b=3Da
puts a.object_id
puts b.object_id
b[2]=3D100
puts b.object_id
puts b.join(",")
puts a.object_id
puts a.join(",")
# 1) when b=3Da, 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.
 
C

Chintan Trivedi

--0-333106451-1135165662=:79062
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

puts "For String"
a=3D"Hello world!"
b=3Da
puts a.object_id
puts b.object_id
b=3D"Bye world!"
puts b.object_id
=20
The object id of b is changing bcz u are redifining b using "=3D" oper=
ator. Instead if u use b.replace("Bye word!") thn the object id will not=
change.=20

puts "For number"
a=3D1
b=3Da
puts a.object_id
puts b.object_id
b=3D2
puts b.object_id
b=3D1
puts b.object_id

#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=3D"Hello world!"
b=3Da
puts a.object_id
puts b.object_id
b=3D"Bye world!"
puts b.object_id
b=3D"Hello world!"
puts b.object_id
b=3D"Why?"
puts b.object_id
b=3D"Why?"
puts b.object_id
puts "Why?".object_id

# 1) When you assign b=3Da, 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=3D[1,2,3,4]
b=3Da
puts a.object_id
puts b.object_id
b[2]=3D100
puts b.object_id
puts b.join(",")
puts a.object_id
puts a.join(",")
# 1) when b=3Da, 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 cle=
ar
explanation.

--
Huazhi Gong (Hank)



__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20
--0-333106451-1135165662=:79062--
 
A

Alexander Jakopin

On Wed, 2005-12-21 at 20:40 +0900, Hank Gong 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.
 
D

Dan Diebolt

--0-1846763520-1135170043=:47127
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Anyone else notice that the object_id of an integer (at least for Fixnum)=
is twice the integer plus 1?
=20
irb(main):030:0> 1.object_id
=3D> 3
irb(main):031:0> 2.object_id
=3D> 5
irb(main):032:0> 6534.object_id
=3D> 13069
irb(main):033:0> 2*6534+1
=3D> 13069
=20
=20

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20
--0-1846763520-1135170043=:47127--
 
D

Devin Mullins

Hank said:
#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.
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(",")


# 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, :eek:mega, str]
str['dog'] = 'cat'
p a #=> [1, 2, 3, 4, "my cat"]
p b #=> [:alpha, :eek:mega, "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, :eek:mega, str]
str = 'my cat'
p a #=> [1, 2, 3, 4, "my dog"]
p b #=> [:alpha, :eek:mega, "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
 
E

Eric Hodel

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

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

Eero Saynatkari

Devin said:
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.
<snip comprehensive explanation />

Devin


E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top