Project

General

Profile

Actions

Misc #21762

open

Box: Inspect improvements

Misc #21762: Box: Inspect improvements

Added by zverok (Victor Shepelev) about 13 hours ago.

Status:
Open
Assignee:
-
[ruby-core:124012]

Description

1. Currently, Ruby::Box provides an #inspect method, and its output is reasonably short yet mysterious:

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 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:

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.

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:

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:

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:

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).

No data to display

Actions

Also available in: PDF Atom