[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

object_id 1, 2, 3

SpringFlowers AutumnMoon

9/25/2007 12:15:00 AM

Fixnum object_id
0 1
1 3
2 5

-1 -1
-2 -3


so the question is... which objects have the object_id 2, 4, 6, and -2,
-4, etc?




irb(main):041:0> a = 0
=> 0
irb(main):042:0> a.object_id
=> 1

irb(main):043:0> a = 1
=> 1
irb(main):044:0> a.object_id
=> 3

irb(main):045:0> a = 2
=> 2
irb(main):046:0> a.object_id
=> 5

irb(main):047:0> a = -1
=> -1
irb(main):048:0> a.object_id
=> -1

irb(main):049:0> a = -2
=> -2
irb(main):050:0> a.object_id
=> -3
--
Posted via http://www.ruby-....

14 Answers

Rob Biedenharn

9/25/2007 12:33:00 AM

0

$ irb
irb(main):001:0> true.object_id
=> 2
irb(main):002:0> nil.object_id
=> 4
irb(main):003:0> false.object_id
=> 0

I saw a flowchart somewhere (online?) that showed how the least-
significant bits were used by the interpreter in the implementation
of some "objects" which were both common and immutable (TrueClass,
FalseClass, NilClass, Fixnum).

The LSB=1 is Fixnum (hence the object_id of n would be (n<<1)|0x01 in
the underlying C code).

-Rob

On Sep 24, 2007, at 8:14 PM, SpringFlowers AutumnMoon wrote:

> Fixnum object_id
> 0 1
> 1 3
> 2 5
>
> -1 -1
> -2 -3
>
>
> so the question is... which objects have the object_id 2, 4, 6,
> and -2,
> -4, etc?
>
>
>
>
> irb(main):041:0> a = 0
> => 0
> irb(main):042:0> a.object_id
> => 1
>
> irb(main):043:0> a = 1
> => 1
> irb(main):044:0> a.object_id
> => 3
>
> irb(main):045:0> a = 2
> => 2
> irb(main):046:0> a.object_id
> => 5
>
> irb(main):047:0> a = -1
> => -1
> irb(main):048:0> a.object_id
> => -1
>
> irb(main):049:0> a = -2
> => -2
> irb(main):050:0> a.object_id
> => -3
> --

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



SpringFlowers AutumnMoon

9/25/2007 12:37:00 AM

0

Rob Biedenharn wrote:
> $ irb
> irb(main):001:0> true.object_id
> => 2
> irb(main):002:0> nil.object_id
> => 4
> irb(main):003:0> false.object_id
> => 0

irb(main):063:0> 1000.object_id
=> 2001

irb(main):064:0> 1001.object_id
=> 2003

wonder where the 2002, 2004, etc go...
--
Posted via http://www.ruby-....

SpringFlowers AutumnMoon

9/25/2007 12:41:00 AM

0

Rob Biedenharn wrote:

> I saw a flowchart somewhere (online?) that showed how the least-
> significant bits were used by the interpreter in the implementation
> of some "objects" which were both common and immutable (TrueClass,
> FalseClass, NilClass, Fixnum).
>
> The LSB=1 is Fixnum (hence the object_id of n would be (n<<1)|0x01 in
> the underlying C code).


you mean the 2002, 2004, 20002, 20004, etc are all for other immutable
objects? what are they i wonder... the negative numbers have their own
negative object_id's.

--
Posted via http://www.ruby-....

Tim Hunter

9/25/2007 1:07:00 AM

0

SpringFlowers AutumnMoon wrote:
> Fixnum object_id
> 0 1
> 1 3
> 2 5
>
> -1 -1
> -2 -3
>
>
> so the question is... which objects have the object_id 2, 4, 6, and -2,
> -4, etc?
>

ex$ irb
irb(main):001:0> x = true
=> true
irb(main):002:0> x.object_id
=> 2
irb(main):003:0> x = false
=> false
irb(main):004:0> x.object_id
=> 0
irb(main):005:0> x = nil
=> nil
irb(main):006:0> x.object_id
=> 4

In other words, very small even values of object_id are reserved for
special values.


--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Rick DeNatale

9/25/2007 5:51:00 PM

0

On 9/24/07, Tim Hunter <TimHunter@nc.rr.com> wrote:
> SpringFlowers AutumnMoon wrote:
> > Fixnum object_id
> > 0 1
> > 1 3
> > 2 5
> >
> > -1 -1
> > -2 -3
> >
> >
> > so the question is... which objects have the object_id 2, 4, 6, and -2,
> > -4, etc?
> >
>
> ex$ irb
> irb(main):001:0> x = true
> => true
> irb(main):002:0> x.object_id
> => 2
> irb(main):003:0> x = false
> => false
> irb(main):004:0> x.object_id
> => 0
> irb(main):005:0> x = nil
> => nil
> irb(main):006:0> x.object_id
> => 4
>
> In other words, very small even values of object_id are reserved for
> special values.

One should keep in mind that these are interesting implementation
artifacts. I don't believe that there's any guarantee that such ids
would be the same in an arbitrary ruby implementation or even between
versions of the same implementation.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Arlen Cuss

9/26/2007 4:09:00 PM

0


> One should keep in mind that these are interesting implementation
> artifacts. I don't believe that there's any guarantee that such ids
> would be the same in an arbitrary ruby implementation or even between
> versions of the same implementation.
>

No -- nor should they be expected to be, under any circumstance.

At best, they're used for comparing objects (Object#==), and should
never be explicitly stored or used for comparisons or hash lookups *in
their own right*. Even two similar strings can have different
object_ids.

irb(main):001:0> "a".object_id
=> 23739988544880
irb(main):002:0> "a".object_id
=> 23739988519760
irb(main):003:0>

Be careful, and remember that you should anticipate them to never remain
constant between executions of even the same interpreter. (though they
will for your numbers, but beware.. :))

Arlen


SpringFlowers AutumnMoon

9/26/2007 8:27:00 PM

0

Rick Denatale wrote:
> On 9/24/07, Tim Hunter <TimHunter@nc.rr.com> wrote:
>> > so the question is... which objects have the object_id 2, 4, 6, and -2,
>> irb(main):004:0> x.object_id
>> => 0
>> irb(main):005:0> x = nil
>> => nil
>> irb(main):006:0> x.object_id
>> => 4

> One should keep in mind that these are interesting implementation
> artifacts. I don't believe that there's any guarantee that such ids
> would be the same in an arbitrary ruby implementation or even between
> versions of the same implementation.

right, it is just from the curiosity standpoint. that where did those
even number object id's go? (the 10002, 10004, etc)

--
Posted via http://www.ruby-....

Rob Biedenharn

9/26/2007 8:57:00 PM

0



Tim Hunter

9/26/2007 9:01:00 PM

0

SpringFlowers AutumnMoon wrote:
> Rick Denatale wrote:
>> On 9/24/07, Tim Hunter <TimHunter@nc.rr.com> wrote:
>>> > so the question is... which objects have the object_id 2, 4, 6, and -2,
>>> irb(main):004:0> x.object_id
>>> => 0
>>> irb(main):005:0> x = nil
>>> => nil
>>> irb(main):006:0> x.object_id
>>> => 4
>
>> One should keep in mind that these are interesting implementation
>> artifacts. I don't believe that there's any guarantee that such ids
>> would be the same in an arbitrary ruby implementation or even between
>> versions of the same implementation.
>
> right, it is just from the curiosity standpoint. that where did those
> even number object id's go? (the 10002, 10004, etc)

Object ids are memory addresses. Due to the way C programs (remember
Ruby is a C program) allocate memory, the object ids that are not
Fixnums and not very small even numbers are (at least in common systems)
always multiples of 8, so 10002 isn't going to be a real object id.
Also, because of the way operating systems dole out memory to C
programs, the memory addresses are usually going to be very large
numbers. (Mostly because operating systems like to reserve low memory
addresses for themselves.) Memory addresses are of course always
positive numbers, but Ruby displays object ids as signed numbers. If the
memory address is sufficiently large (such that the sign bit is set)
Ruby displays it as a negative number.

Of course at this point somebody will pipe up and say that what I just
wrote is untrue on such-and-such an obscure computer, and they'd be
right. I'm just talking about the kind of computers (PCs) and operating
systems (Win, Linux, etc.) that Ruby commonly runs on. Personally I'd be
interested in hearing about computers that Ruby runs on that don't act
this way.
--
Posted via http://www.ruby-....

Ryan Davis

9/27/2007 7:29:00 AM

0


On Sep 24, 2007, at 17:14 , SpringFlowers AutumnMoon wrote:

> Fixnum object_id
> 0 1
> 1 3
> 2 5
>
> -1 -1
> -2 -3
>
>
> so the question is... which objects have the object_id 2, 4, 6,
> and -2,
> -4, etc?

ri ObjectSpace._id2ref

>> (0..5).map { |n| ObjectSpace._id2ref(n) }
=> [false, 0, true, 1, nil, 2]