Feature #5805

object_hexid

Added by Thomas Sawyer over 2 years ago. Updated over 1 year ago.

[ruby-core:41797]
Status:Feedback
Priority:Normal
Assignee:-
Category:-
Target version:next minor

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!

History

#1 Updated by Robert A. Heiler over 2 years ago

I think this seems like a simple proposal.

#2 Updated by Nobuyoshi Nakada over 2 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 objecthexid
"0x"+[object
id<!").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?

#3 Updated by Shyouhei Urabe over 2 years ago

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

In 1.9.3 or later
module Kernel
def objecthexid
"0x"+[object
id<!").unpack("H*")[0]
end
end

I prefer to use sprintf() instead.

#4 Updated by Thomas Sawyer over 2 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.

#5 Updated by Thomas Sawyer over 2 years ago

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

In 1.9.3 or later
module Kernel
def objecthexid
"0x"+[object
id<!").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"
testobjecthexid.rb:9
07 test do
08 o = Object.new
=> 09 "#Object:#{o.object_hexid}".assert == o.inspect
10 end

#6 Updated by Nobuyoshi Nakada over 2 years ago

=begin

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})).
=end

#7 Updated by Thomas Sawyer over 2 years ago

Sorry, how does super help?

#8 Updated by Nikolai Weibull over 2 years ago

On Sun, Jan 8, 2012 at 17:33, Thomas Sawyer transfire@gmail.com wrote:

Sorry, how does super help?

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

perhaps?

#9 Updated by Yusuke Endoh over 1 year ago

  • Target version set to next minor

Also available in: Atom PDF