Project

General

Profile

Actions

Feature #5805

closed

object_hexid

Added by trans (Thomas Sawyer) over 12 years ago. Updated over 6 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:41797]

Description

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!

Actions #1

Updated by shevegen (Robert A. Heiler) over 12 years ago

I think this seems like a simple proposal.

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

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

Updated by shyouhei (Shyouhei Urabe) over 12 years ago

On 2011年12月25日 14:38, Nobuyoshi Nakada wrote:

In 1.9.3 or later

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

I prefer to use sprintf() instead.

Updated by trans (Thomas Sawyer) over 12 years ago

Thank you for 1.9.3+ definition, I will add that. Do you happen to know definition for 1.8.7? I am not worried about anything older than that.

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?

That's true. But if the definition should ever change again, this issue will come up again. So I think still better that Ruby make it's definition accessible.

Updated by trans (Thomas Sawyer) over 12 years ago

On 2011年12月25日 14:38, Nobuyoshi Nakada wrote:

In 1.9.3 or later

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

Hmm.. this seems to pad two extra zeros.

"0x00000000014100e0"

instead of

"0x000000014100e0"

Is Urabe correct? Does sprintf work just as well? I don't know #pack and Ruby internals enough to be sure.

Also, here is 1.8.7 test failure.

(assertion) a == b
a) "#<Object:0x..fb710eb4c>"
b) "#<Object:0xb710eb4c>"
    test_object_hexid.rb:9
    07     test do
    08       o = Object.new
 => 09       "#<Object:#{o.object_hexid}>".assert == o.inspect
    10     end

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

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!

You can use super.

Updated by trans (Thomas Sawyer) about 12 years ago

Sorry, how does super help?

Updated by now (Nikolai Weibull) about 12 years ago

On Sun, Jan 8, 2012 at 17:33, Thomas Sawyer wrote:

Sorry, how does super help?

   def inspect
     '#<IWhoNeedAHexID: %s>' % super.sub(/\A.*(0x[0-9a-f]+).*/, '\\1')
   end

perhaps?

Updated by mame (Yusuke Endoh) over 11 years ago

  • Target version set to 2.6
Actions #10

Updated by naruse (Yui NARUSE) over 6 years ago

  • Target version deleted (2.6)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0