Issue #6130 has been updated by Eregon (Benoit Daloze).

File 0001-object.c-rb_obj_inspect-Kernel-inspect-improve-consi.patch added

Eregon (Benoit Daloze) wrote:
> I've been working on this and wondered what to do if there is no instance variable. Should it call (dynamically) #to_s? or always use Kernel#to_s which gives the class name and pointer (which would be called anyway if #to_s was not overridden) ?

After some thoughts I believe it would not be worth changing the behavior if it was not fully consistent.
So I propose this patch.

test-all pass with only two modifications in pretty_print tests, which relied on #inspect calling #to_s.
The internal structures have been updated so #inspect is an alias of #to_s when it was not defined.
The last modification is to define #inspect on the toplevel self (only #to_s was defined).

Of course, tests for the new behavior need to be added (I'll do later if the idea is approved).

What do you think?
----------------------------------------
Feature #6130: inspect using to_s is pain
https://bugs.ruby-lang.org/issues/6130#change-25819

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: mame (Yusuke Endoh)
Category: core
Target version: 2.0.0


Every time I define #to_s on a class I have to remember to also redefine #inspect. It's annoying and has led me to waste hours debugging b/c the errors that can occur from it often seem strange and unrelated.

I think #inspect should have an independent definition that outputs the class name, object_id and when possible instance variable settings, and should have nothing to do with #to_s by default. We developers can always alias it as such if it is appropriate.

The only exception should be for classes that have literal representation in Ruby, such as Array, String, Hash, etc. In those cases the #inspect should give the literal representation.




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