Does the ruby ​​object_id method use a memory cell?

Or does this method simply indicate the unique integer that each object has?

+7
ruby
source share
5 answers

This is a combination of many parameters, value, type of object, memory location.
More details can be read here.

+6
source share

This is not a direct reference to a memory location, but "coding" refers to a particular Ruby implementation. If you can read the C code, you can participate in the rb_obj_id and id2ref in gc.c in the Ruby 1.8.6 source. You can also learn more about the "encoding" in the "Objects Embedded in VALUE" section of the Partial Translation Chapter 2 on the Ruby Hacking Guide ,

+4
source share

It is worth noting that you can perform a reverse search for object identifiers using:

 ObjectSpace._id2ref(object_id) 

For example:

 ObjectSpace._id2ref(0) #=> false ObjectSpace._id2ref(1) #=> 0 ObjectSpace._id2ref(2) #=> true ObjectSpace._id2ref(3) #=> 1 ObjectSpace._id2ref(4) #=> nil 
+3
source share

well, that depends on what you mean by "ruby";) In jruby, it's just a unique integer as far as I can tell.

Also, things like numbers are not a memory location. I forget all the details and I’m sure that someone will give them to you.

 irb(main):020:0> 1.object_id => 3 irb(main):021:0> (2-1).object_id => 3 
+1
source share

In a β€œnormal” ruby ​​(MRI 1.8.x and 1.9.x), this is simply a unique value.

This also applies to IronRuby.

+1
source share

All Articles