[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 #=> #<Namespace:3,user,optional> ``` 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 # #<#<Class:0x0000788614d88340>:0x000078861444b0e8> # or, more realistically: raise "Something bad happened in #{b}" # Something bad happened in #<#<Class:0x0000788614d88340>: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') #=> #<#<Class:0x00007750c2e9d0d8>: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 `<Ruby::Box:1>::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 #=> #<Class:0x00007ec030697ef8> 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/
Issue #21762 has been updated by mame (Yusuke Endoh). Status changed from Open to Assigned Assignee set to tagomoris (Satoshi Tagomori) ---------------------------------------- Misc #21762: Box: Inspect improvements https://bugs.ruby-lang.org/issues/21762#change-115528 * Author: zverok (Victor Shepelev) * Status: Assigned * Assignee: tagomoris (Satoshi Tagomori) ---------------------------------------- 1\. Currently, `Ruby::Box` provides an `#inspect` method, and its output is reasonably short yet mysterious: ```ruby Ruby::Box.new #=> #<Namespace:3,user,optional> ``` 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 # #<#<Class:0x0000788614d88340>:0x000078861444b0e8> # or, more realistically: raise "Something bad happened in #{b}" # Something bad happened in #<#<Class:0x0000788614d88340>: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') #=> #<#<Class:0x00007750c2e9d0d8>: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 `<Ruby::Box:1>::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 #=> #<Class:0x00007ec030697ef8> 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/
participants (2)
-
mame (Yusuke Endoh) -
zverok (Victor Shepelev)