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

ruby-changes:14137

From: yugui <ko1@a...>
Date: Fri, 27 Nov 2009 11:55:50 +0900 (JST)
Subject: [ruby-changes:14137] Ruby:r25951 (ruby_1_9_1): merges r25122 from trunk into ruby_1_9_1.

yugui	2009-11-27 11:55:32 +0900 (Fri, 27 Nov 2009)

  New Revision: 25951

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

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

  Modified files:
    branches/ruby_1_9_1/ChangeLog
    branches/ruby_1_9_1/lib/pp.rb
    branches/ruby_1_9_1/test/test_pp.rb
    branches/ruby_1_9_1/version.h

Index: ruby_1_9_1/ChangeLog
===================================================================
--- ruby_1_9_1/ChangeLog	(revision 25950)
+++ ruby_1_9_1/ChangeLog	(revision 25951)
@@ -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]
+
 Sat Sep 26 08:35:12 2009  Koichi Sasada  <ko1@a...>
 
 	* iseq.c (compile_string): rename to parse_string(), because
Index: ruby_1_9_1/lib/pp.rb
===================================================================
--- ruby_1_9_1/lib/pp.rb	(revision 25950)
+++ ruby_1_9_1/lib/pp.rb	(revision 25951)
@@ -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
@@ -515,186 +529,4 @@
     end
   }
 }
-
 # :enddoc:
-if __FILE__ == $0
-  require 'test/unit'
-
-  class PPTest < Test::Unit::TestCase
-    def test_list0123_12
-      assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12))
-    end
-
-    def test_list0123_11
-      assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11))
-    end
-
-    OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
-    def test_struct_override_members # [ruby-core:7865]
-      a = OverriddenStruct.new(1,2)
-      assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''))
-    end
-
-    def test_redefined_method
-      o = ""
-      def o.method
-      end
-      assert_equal(%(""\n), PP.pp(o, ""))
-    end
-  end
-
-  class HasInspect
-    def initialize(a)
-      @a = a
-    end
-
-    def inspect
-      return "<inspect:#{@a.inspect}>"
-    end
-  end
-
-  class HasPrettyPrint
-    def initialize(a)
-      @a = a
-    end
-
-    def pretty_print(q)
-      q.text "<pretty_print:"
-      q.pp @a
-      q.text ">"
-    end
-  end
-
-  class HasBoth
-    def initialize(a)
-      @a = a
-    end
-
-    def inspect
-      return "<inspect:#{@a.inspect}>"
-    end
-
-    def pretty_print(q)
-      q.text "<pretty_print:"
-      q.pp @a
-      q.text ">"
-    end
-  end
-
-  class PrettyPrintInspect < HasPrettyPrint
-    alias inspect pretty_print_inspect
-  end
-
-  class PrettyPrintInspectWithoutPrettyPrint
-    alias inspect pretty_print_inspect
-  end
-
-  class PPInspectTest < Test::Unit::TestCase
-    def test_hasinspect
-      a = HasInspect.new(1)
-      assert_equal("<inspect:1>\n", PP.pp(a, ''))
-    end
-
-    def test_hasprettyprint
-      a = HasPrettyPrint.new(1)
-      assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
-    end
-
-    def test_hasboth
-      a = HasBoth.new(1)
-      assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
-    end
-
-    def test_pretty_print_inspect
-      a = PrettyPrintInspect.new(1)
-      assert_equal("<pretty_print:1>", a.inspect)
-      a = PrettyPrintInspectWithoutPrettyPrint.new
-      assert_raise(RuntimeError) { a.inspect }
-    end
-
-    def test_proc
-      a = proc {1}
-      assert_equal("#{a.inspect}\n", PP.pp(a, ''))
-    end
-
-    def test_to_s_with_iv
-      a = Object.new
-      def a.to_s() "aaa" end
-      a.instance_eval { @a = nil }
-      result = PP.pp(a, '')
-      assert_equal("#{a.inspect}\n", result)
-      assert_match(/\A#<Object.*>\n\z/m, result)
-      a = 1.0
-      a.instance_eval { @a = nil }
-      result = PP.pp(a, '')
-      assert_equal("#{a.inspect}\n", result)
-    end
-
-    def test_to_s_without_iv
-      a = Object.new
-      def a.to_s() "aaa" end
-      result = PP.pp(a, '')
-      assert_equal("#{a.inspect}\n", result)
-      assert_equal("aaa\n", result)
-    end
-  end
-
-  class PPCycleTest < Test::Unit::TestCase
-    def test_array
-      a = []
-      a << a
-      assert_equal("[[...]]\n", PP.pp(a, ''))
-      assert_equal("#{a.inspect}\n", PP.pp(a, ''))
-    end
-
-    def test_hash
-      a = {}
-      a[0] = a
-      assert_equal("{0=>{...}}\n", PP.pp(a, ''))
-      assert_equal("#{a.inspect}\n", PP.pp(a, ''))
-    end
-
-    S = Struct.new("S", :a, :b)
-    def test_struct
-      a = S.new(1,2)
-      a.b = a
-      assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, ''))
-      assert_equal("#{a.inspect}\n", PP.pp(a, ''))
-    end
-
-    def test_object
-      a = Object.new
-      a.instance_eval {@a = a}
-      assert_equal(a.inspect + "\n", PP.pp(a, ''))
-    end
-
-    def test_anonymous
-      a = Class.new.new
-      assert_equal(a.inspect + "\n", PP.pp(a, ''))
-    end
-
-    def test_withinspect
-      a = []
-      a << HasInspect.new(a)
-      assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''))
-      assert_equal("#{a.inspect}\n", PP.pp(a, ''))
-    end
-
-    def test_share_nil
-      begin
-        PP.sharing_detection = true
-        a = [nil, nil]
-        assert_equal("[nil, nil]\n", PP.pp(a, ''))
-      ensure
-        PP.sharing_detection = false
-      end
-    end
-  end
-
-  class PPSingleLineTest < Test::Unit::TestCase
-    def test_hash
-      assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699]
-      assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''))
-    end
-  end
-end
Index: ruby_1_9_1/version.h
===================================================================
--- ruby_1_9_1/version.h	(revision 25950)
+++ ruby_1_9_1/version.h	(revision 25951)
@@ -1,5 +1,5 @@
 #define RUBY_VERSION "1.9.1"
-#define RUBY_PATCHLEVEL 352
+#define RUBY_PATCHLEVEL 353
 #define RUBY_VERSION_MAJOR 1
 #define RUBY_VERSION_MINOR 9
 #define RUBY_VERSION_TEENY 1
Index: ruby_1_9_1/test/test_pp.rb
===================================================================
--- ruby_1_9_1/test/test_pp.rb	(revision 25950)
+++ ruby_1_9_1/test/test_pp.rb	(revision 25951)
@@ -1,4 +1,192 @@
-require 'pathname'
-require Pathname.new(__FILE__).dirname.join('inlinetest.rb')
-target = __FILE__[/test_(.*\.rb)$/, 1]
-InlineTest.loadtest(target)
+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))
+  end
+
+  def test_list0123_11
+    assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11))
+  end
+
+  OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
+  def test_struct_override_members # [ruby-core:7865]
+    a = OverriddenStruct.new(1,2)
+    assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''))
+  end
+
+  def test_redefined_method
+    o = ""
+    def o.method
+    end
+    assert_equal(%(""\n), PP.pp(o, ""))
+  end
+end
+
+class HasInspect
+  def initialize(a)
+    @a = a
+  end
+
+  def inspect
+    return "<inspect:#{@a.inspect}>"
+  end
+end
+
+class HasPrettyPrint
+  def initialize(a)
+    @a = a
+  end
+
+  def pretty_print(q)
+    q.text "<pretty_print:"
+    q.pp @a
+    q.text ">"
+  end
+end
+
+class HasBoth
+  def initialize(a)
+    @a = a
+  end
+
+  def inspect
+    return "<inspect:#{@a.inspect}>"
+  end
+
+  def pretty_print(q)
+    q.text "<pretty_print:"
+    q.pp @a
+    q.text ">"
+  end
+end
+
+class PrettyPrintInspect < HasPrettyPrint
+  alias inspect pretty_print_inspect
+end
+
+class PrettyPrintInspectWithoutPrettyPrint
+  alias inspect pretty_print_inspect
+end
+
+class PPInspectTest < Test::Unit::TestCase
+  def test_hasinspect
+    a = HasInspect.new(1)
+    assert_equal("<inspect:1>\n", PP.pp(a, ''))
+  end
+
+  def test_hasprettyprint
+    a = HasPrettyPrint.new(1)
+    assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
+  end
+
+  def test_hasboth
+    a = HasBoth.new(1)
+    assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
+  end
+
+  def test_pretty_print_inspect
+    a = PrettyPrintInspect.new(1)
+    assert_equal("<pretty_print:1>", a.inspect)
+    a = PrettyPrintInspectWithoutPrettyPrint.new
+    assert_raise(RuntimeError) { a.inspect }
+  end
+
+  def test_proc
+    a = proc {1}
+    assert_equal("#{a.inspect}\n", PP.pp(a, ''))
+  end
+
+  def test_to_s_with_iv
+    a = Object.new
+    def a.to_s() "aaa" end
+    a.instance_eval { @a = nil }
+    result = PP.pp(a, '')
+    assert_equal("#{a.inspect}\n", result)
+    assert_match(/\A#<Object.*>\n\z/m, result)
+    a = 1.0
+    a.instance_eval { @a = nil }
+    result = PP.pp(a, '')
+    assert_equal("#{a.inspect}\n", result)
+  end
+
+  def test_to_s_without_iv
+    a = Object.new
+    def a.to_s() "aaa" end
+    result = PP.pp(a, '')
+    assert_equal("#{a.inspect}\n", result)
+    assert_equal("aaa\n", result)
+  end
+end
+
+class PPCycleTest < Test::Unit::TestCase
+  def test_array
+    a = []
+    a << a
+    assert_equal("[[...]]\n", PP.pp(a, ''))
+    assert_equal("#{a.inspect}\n", PP.pp(a, ''))
+  end
+
+  def test_hash
+    a = {}
+    a[0] = a
+    assert_equal("{0=>{...}}\n", PP.pp(a, ''))
+    assert_equal("#{a.inspect}\n", PP.pp(a, ''))
+  end
+
+  S = Struct.new("S", :a, :b)
+  def test_struct
+    a = S.new(1,2)
+    a.b = a
+    assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, ''))
+    assert_equal("#{a.inspect}\n", PP.pp(a, ''))
+  end
+
+  def test_object
+    a = Object.new
+    a.instance_eval {@a = a}
+    assert_equal(a.inspect + "\n", PP.pp(a, ''))
+  end
+
+  def test_anonymous
+    a = Class.new.new
+    assert_equal(a.inspect + "\n", PP.pp(a, ''))
+  end
+
+  def test_withinspect
+    a = []
+    a << HasInspect.new(a)
+    assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''))
+    assert_equal("#{a.inspect}\n", PP.pp(a, ''))
+  end
+
+  def test_share_nil
+    begin
+      PP.sharing_detection = true
+      a = [nil, nil]
+      assert_equal("[nil, nil]\n", PP.pp(a, ''))
+    ensure
+      PP.sharing_detection = false
+    end
+  end
+end
+
+class PPSingleLineTest < Test::Unit::TestCase
+  def test_hash
+    assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699]
+    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/

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