ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nate.berkopec@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100451] [Ruby master Misc#17199] id outputed by inspect and to_s output does not allow to find actual object_id and vice-versa
Date: Tue, 20 Oct 2020 20:15:03 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-88070.20201020201503.44605@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17199.20200928114039.44605@ruby-lang.org

Issue #17199 has been updated by nateberkopec (Nate Berkopec).


This looks like an oversight to me? I agree with Benoit - showing object_id everywhere we used to show address seems the best way forward.

----------------------------------------
Misc #17199: id outputed by inspect and to_s output does not allow to find actual object_id and vice-versa
https://bugs.ruby-lang.org/issues/17199#change-88070

* Author: Annih (Baptiste Courtois)
* Status: Open
* Priority: Normal
----------------------------------------
Hello, here is my first ruby issue sorry in advance if it is incorrectly filled.

# Issue

The value returned by `#object_id` is not aligned anymore with displayed info in `#inspect` and `#to_s` methods.

## with ruby < 2.7

``` ruby
Object.new.tap { |o| p "#to_s=#{o.to_s}, #inspect=#{o.inspect}, #__id__=#{o.__id__}, shifted_id=#{(o.__id__ << 1).to_s(16)}" }
"#to_s=#<Object:0x0000000000d202a8>, #inspect=#<Object:0x0000000000d202a8>, #__id__=6881620, shifted_id=d202a8"
```

## with ruby >= 2.7

``` ruby
 Object.new.tap { |o| p "#to_s=#{o.to_s}, #inspect=#{o.inspect}, #__id__=#{o.__id__}, shifted_id=#{(o.__id__ << 1).to
s(16)}" }
"#to_s=#<Object:0x0000555dc8640b88>, #inspect=#<Object:0x0000555dc8640b88>, #__id__=220, shifted_id=1b8"
```


# Consequences

It makes harder:
- to implement a clean override of the `#inspect` method. i.e. How to keep the same output without ability to compute to the same "object_id" value.
- to debug the object using the inspect output. i.e. `ObjectSpace._id2ref(id_from_inspect >> 1)` used to work, now it doesn't (`RangeError: <xXx> is not id value`).

# Suggestion

IMHO either:
- the `#to_s` and `#inspect` documentation are obsolete `The default [...] [shows|prints] [...] an encoding of the object id` and the change could have been a bit more advertised
- they should use the result of `#object_id` instead of displaying the object pointer address

Another solution could be to provide a method to get access to the address, but I'm not sure you want that.

P.S. While debugging my problem I found [this ruby-forum thread](https://www.ruby-forum.com/t/understanding-object-id-in-ruby-2-7/260268/4) where people dived a bit more than me into ruby's code.



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2020-10-20 20:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-28 11:40 [ruby-core:100200] [Ruby master Misc#17199] id outputed by inspect and to_s output does not allow to find actual object_id and vice-versa baptiste.courtois
2020-10-20 15:55 ` [ruby-core:100446] " chris
2020-10-20 18:31 ` [ruby-core:100448] " johnson.joel.b
2020-10-20 20:06 ` [ruby-core:100450] " eregontp
2020-10-20 20:15 ` nate.berkopec [this message]
2020-10-20 21:32 ` [ruby-core:100458] " tenderlove
2020-10-20 21:35 ` [ruby-core:100459] " tenderlove

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-88070.20201020201503.44605@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).