Misc #21762
openBox: Inspect improvements
Description
1. Currently, Ruby::Box provides an #inspect method, and its output is reasonably short yet mysterious:
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.
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:
Which is confusing, because it looks like a constant that can be referred from the outer scope, but it is not:
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:
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).
Updated by mame (Yusuke Endoh) 7 months ago
- Status changed from Open to Assigned
- Assignee set to tagomoris (Satoshi Tagomori)