mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/pp.rb (PP:ObjectMixin#pretty_print): delegates has no inspect
method. [ruby-core:25804] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25122 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
37c552e550
commit
4319c0235d
3 changed files with 34 additions and 2 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Sun Sep 27 13:06:43 2009 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
Sun Sep 27 12:01:42 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* string.c (str_buf_cat2): optimize since all second arguments are
|
* string.c (str_buf_cat2): optimize since all second arguments are
|
||||||
|
|
18
lib/pp.rb
18
lib/pp.rb
|
@ -283,9 +283,23 @@ class PP < PrettyPrint
|
||||||
# This module provides predefined #pretty_print methods for some of
|
# This module provides predefined #pretty_print methods for some of
|
||||||
# the most commonly used built-in classes for convenience.
|
# the most commonly used built-in classes for convenience.
|
||||||
def pretty_print(q)
|
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
|
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
|
q.text self.to_s
|
||||||
else
|
else
|
||||||
q.pp_object(self)
|
q.pp_object(self)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
require 'pp'
|
require 'pp'
|
||||||
|
require 'delegate'
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
|
||||||
|
module PPTestModule
|
||||||
|
|
||||||
class PPTest < Test::Unit::TestCase
|
class PPTest < Test::Unit::TestCase
|
||||||
def test_list0123_12
|
def test_list0123_12
|
||||||
assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12))
|
assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12))
|
||||||
|
@ -178,3 +181,13 @@ class PPSingleLineTest < Test::Unit::TestCase
|
||||||
assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''))
|
assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''))
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue