From: "zverok (Victor Shepelev) via ruby-core" Date: 2025-12-03T19:53:38+00:00 Subject: [ruby-core:124012] [Ruby Misc#21762] Box: Inspect improvements Issue #21762 has been reported by zverok (Victor Shepelev). ---------------------------------------- Misc #21762: Box: Inspect improvements https://bugs.ruby-lang.org/issues/21762 * Author: zverok (Victor Shepelev) * Status: Open ---------------------------------------- 1\. Currently, `Ruby::Box` provides an `#inspect` method, and its output is reasonably short yet mysterious: ```ruby Ruby::Box.new #=> # ``` First, it uses old `Namespace` name instead of `Ruby::Box`. Next, I am not sure how to interpret everything that goes after the class name (reading through `#inspect` [code](https://docs.ruby-lang.org/en/master/Ruby/Box.html#method-i-inspect) gives some insight, bug I am still unsure what should "user" or "optional" mean). 2\. Box doesn't redefine `#to_s`, which leads to this: ```ruby b = Ruby::Box.new puts b # #<#:0x000078861444b0e8> # or, more realistically: raise "Something bad happened in #{b}" # Something bad happened in #<#:0x000078861444b0e8> (RuntimeError) ``` I think that making `#to_s` an alias to `#inspect` would be reasonable. 3\. In Ruby 4.0.0-preview2, `#inspect` of nested classes was prefixed with `#to_s` of the box. ```ruby b.require('cgi') b.eval('CGI') #=> #<#:0x00007750c20050c0>::CGI ``` While with the current `#to_s` it looked weird, if (2) is fixed, it could become quite reasonable (i.e. if it looked like `::CGI`). In the current master, the behavior had changed: ```ruby b.require('cgi') b.eval('CGI') #=> CGI ``` Which is confusing, because it looks like a constant that can be referred from the outer scope, but it is not: ```ruby CGI # uninitialized constant CGI (NameError) require 'cgi' CGI == b.eval('CGI') #=> false ``` Can we reconsider changing inspect back to scoped one? I understand there might be some downsides (like if some library checks the literal value of `#inspect` of something to make decisions about the class identity), but as `Box` is experimental anyway, now might be a good time to investigate upsides/downsides?.. 4\. That's a completely optional proposal, but if the `#inspect`/`#to_s` improvements would be made, can we consider (if it is possible) respecting the Box instance being assigned to a constant? Like it happens with dynamically-generated classes: ```ruby c = Class.new #=> # C = c # class receives a name c #=> C ``` Together with my point (3), this might lead to pretty clear naming conventions visible in logs and other places, like `OldAPI::SomeLibrary` (where `OldAPI` is a name of a Box, and `SomeLibrary` is loaded there). -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/