[前][次][番号順一覧][スレッド一覧]

ruby-changes:13356

From: akr <ko1@a...>
Date: Sun, 27 Sep 2009 13:08:46 +0900 (JST)
Subject: [ruby-changes:13356] Ruby:r25122 (trunk): * lib/pp.rb (PP:ObjectMixin#pretty_print): delegates has no inspect

akr	2009-09-27 13:08:31 +0900 (Sun, 27 Sep 2009)

  New Revision: 25122

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25122

  Log:
    * lib/pp.rb (PP:ObjectMixin#pretty_print): delegates has no inspect
      method.  [ruby-core:25804]

  Modified files:
    trunk/ChangeLog
    trunk/lib/pp.rb
    trunk/test/test_pp.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 25121)
+++ ChangeLog	(revision 25122)
@@ -1,3 +1,8 @@
+Sun Sep 27 13:06:43 2009  Tanaka Akira  <akr@f...>
+
+	* lib/pp.rb (PP:ObjectMixin#pretty_print): delegates has no inspect
+	  method.  [ruby-core:25804]
+
 Sun Sep 27 12:01:42 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* string.c (str_buf_cat2): optimize since all second arguments are
Index: lib/pp.rb
===================================================================
--- lib/pp.rb	(revision 25121)
+++ lib/pp.rb	(revision 25122)
@@ -283,10 +283,24 @@
     # This module provides predefined #pretty_print methods for some of
     # the most commonly used built-in classes for convenience.
     def pretty_print(q)
-      if /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:inspect).inspect
+      method_method = Object.instance_method(:method).bind(self)
+      begin
+        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 /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:to_s).inspect && instance_variables.empty?
+      elsif !inspect_method && self.respond_to?(:inspect)
+        q.text self.inspect
+      elsif to_s_method && /\(Kernel\)#/ !~ to_s_method.inspect &&
+            instance_variables.empty?
         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
Index: test/test_pp.rb
===================================================================
--- test/test_pp.rb	(revision 25121)
+++ test/test_pp.rb	(revision 25122)
@@ -1,6 +1,9 @@
 require 'pp'
+require 'delegate'
 require 'test/unit'
 
+module PPTestModule
+
 class PPTest < Test::Unit::TestCase
   def test_list0123_12
     assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12))
@@ -178,3 +181,13 @@
     assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''))
   end
 end
+
+class PPDelegateTest < Test::Unit::TestCase
+  class A < DelegateClass(Array); end
+
+  def test_delegate
+    assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]")
+  end
+end
+
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]