Issue #5805 has been updated by Nobuyoshi Nakada.

Status changed from Open to Feedback

Hi,

(11/12/25 0:22), Thomas Sawyer wrote:
> By default Ruby will show this id when using #inspect.
> 
>   Object.new.inspect
>   => "#<Object:0x000000023cadf0>"
> 
> I, for one, have often wanted to include this hex id when I customize an #inspect method for a class. But despite my lengthy efforts I have never been able to ensure the hex id is correct. It seems to vary a great deal depending on platform and Ruby version.

In 1.9.3 or later
  module Kernel
    def object_hexid
      "0x"+[object_id<<1].pack("L>!").unpack("H*")[0]
    end
  end

> You can see the current effort at this here: https://github.com/rubyworks/facets/blob/master/lib/core/facets/kernel/object_hexid.rb But this definition is currently failing on Ruby 1.8.7 and JRuby. I have tried a number of variations, but nothing sees to work for all platforms.

Even if the method were added, it would never affect 1.9 or earlier, so it changes nothing for 1.8.7.
Is the target of your library only 2.0 or later?

----------------------------------------
Feature #5805: object_hexid
https://bugs.ruby-lang.org/issues/5805

Author: Thomas Sawyer
Status: Feedback
Priority: Normal
Assignee: 
Category: 
Target version: 


I would like to see #object_hexid added to Ruby to return an object's id in hexidecimal form.

By default Ruby will show this id when using #inspect.

  Object.new.inspect
  => "#<Object:0x000000023cadf0>"

I, for one, have often wanted to include this hex id when I customize an #inspect method for a class. But despite my lengthy efforts I have never been able to ensure the hex id is correct. It seems to vary a great deal depending on platform and Ruby version.

You can see the current effort at this here: https://github.com/rubyworks/facets/blob/master/lib/core/facets/kernel/object_hexid.rb But this definition is currently failing on Ruby 1.8.7 and JRuby. I have tried a number of variations, but nothing sees to work for all platforms.

My last ditch approach will be to bind Kernel#inspect InstanceMethod to the current object and extract the hex id from it's output. It should work, but it's definitely a hack. In the future I'd much rather just ask Ruby for it!




-- 
http://redmine.ruby-lang.org