Forum: Ruby A interesting obervation about object_id method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf377e753590af4dfda311132ea50de4?d=identicon&s=25 Hank Gong (Guest)
on 2005-12-21 12:42
(Received via mailing list)
#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.
0d68b919632b9a650522ccadd7d01d78?d=identicon&s=25 Chintan Trivedi (Guest)
on 2005-12-21 12:48
(Received via mailing list)
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 Gong <hankgong@gmail.com> 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)
083c7b239d0919f395a4bbfa5a339f6f?d=identicon&s=25 Alexander Jakopin (Guest)
on 2005-12-21 13:18
(Received via mailing list)
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.
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2005-12-21 14:03
(Received via mailing list)
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
918c6daad03c85e51ad1a11f57017947?d=identicon&s=25 Devin Mullins (Guest)
on 2005-12-21 15:12
(Received via mailing list)
Hank Gong 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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2005-12-21 22:32
(Received via mailing list)
On Dec 21, 2005, at 5:00 AM, Dan Diebolt 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 Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2005-12-21 23:39
Devin Mullins wrote:
> Hank Gong 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.

> <snip comprehensive explanation />
>
> Devin


E
This topic is locked and can not be replied to.