A interesting obervation about object_id method

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

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)

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

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

···

On Wed, 2005-12-21 at 20:40 +0900, Hank Gong wrote:

--
Huazhi Gong (Hank)

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

···

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

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.

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

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

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.

···

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

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

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

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

···

--
Posted via http://www.ruby-forum.com/\.