Issue #6130 has been updated by mame (Yusuke Endoh).


Hello,

> > So I propose this patch.
>
> I'll review.  Please wait!

Looks good to me.  I'll commit it, except the change of test/test_pp.rb.  Thanks!
(It surprised me that your patch also passes rubyspec; rubyspec has no test for this behavior?)


Tanaka-san, I made a patch for pp.  Unless there is no objection, I'll commit this too.



diff --git a/lib/pp.rb b/lib/pp.rb
index 94269ab..6e0c797 100644
--- a/lib/pp.rb
+++ b/lib/pp.rb
@@ -265,8 +265,7 @@ class PP < PrettyPrint
   module ObjectMixin
     # 1. specific pretty_print
     # 2. specific inspect
-    # 3. specific to_s
-    # 4. generic pretty_print
+    # 3. generic pretty_print
 
     # A default pretty printing method for general objects.
     # It calls #pretty_print_instance_variables to list instance variables.
@@ -283,18 +282,10 @@ class PP < PrettyPrint
         inspect_method = method_method.call(:inspect)
       rescue NameError
       end
-      begin
-        to_s_method = method_method.call(:to_s)
-      rescue NameError
-      end
       if inspect_method && /\(Kernel\)#/ !~ inspect_method.inspect
         q.text self.inspect
       elsif !inspect_method && self.respond_to?(:inspect)
         q.text self.inspect
-      elsif to_s_method && /\(Kernel\)#/ !~ to_s_method.inspect
-        q.text self.to_s
-      elsif !to_s_method && self.respond_to?(:to_s)
-        q.text self.to_s
       else
         q.pp_object(self)
       end
diff --git a/test/test_pp.rb b/test/test_pp.rb
index fe65287..acd3e83 100644
--- a/test/test_pp.rb
+++ b/test/test_pp.rb
@@ -118,7 +118,6 @@ class PPInspectTest < Test::Unit::TestCase
     def a.to_s() "aaa" end
     result = PP.pp(a, '')
     assert_equal("#{a.inspect}\n", result)
-    assert_equal("aaa\n", result)
   end
 end
 

-- 
Yusuke Endoh <mame / tsg.ne.jp>
----------------------------------------
Feature #6130: inspect using to_s is pain
https://bugs.ruby-lang.org/issues/6130#change-26053

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/