summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-10 20:53:58 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-10 20:53:58 +0000
commit93aa87568b5fcd0fd03dd8c9f6fed5ed46e23891 (patch)
treedb3fe049aa4d626b1e7daaa2e52ea70ee48ea965
parent4b25d0d2cb5e47907c3586ba86646b25bd25ceaf (diff)
* lib/pp.rb (PP::ObjectMixin#pretty_print): refine to_s handling.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3779 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--lib/pp.rb29
2 files changed, 28 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 63bddcd4d9..84dc726dcb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Sun May 11 05:50:39 2003 Tanaka Akira <akr@m17n.org>
+
+ * lib/pp.rb (PP::ObjectMixin#pretty_print): refine to_s handling.
+
Sat May 10 19:00:08 2003 Takaaki Uematsu <uema2x@jcom.home.ne.jp>
* wince/string.c: file removed.
diff --git a/lib/pp.rb b/lib/pp.rb
index e77f563500..8eb9ce4afc 100644
--- a/lib/pp.rb
+++ b/lib/pp.rb
@@ -239,13 +239,15 @@ class PP < PrettyPrint
module ObjectMixin
# 1. specific pretty_print
- # 2. specific inspect or to_s
- # 3. generic pretty_print
+ # 2. specific inspect
+ # 3. specific to_s if instance variable is empty
+ # 4. generic pretty_print
def pretty_print(pp)
- if /\(Kernel\)#/ !~ method(:inspect).inspect ||
- /\(Kernel\)#/ !~ method(:to_s).inspect
- pp.text inspect
+ if /\(Kernel\)#/ !~ method(:inspect).inspect
+ pp.text self.inspect
+ elsif /\(Kernel\)#/ !~ method(:to_s).inspect && instance_variables.empty?
+ pp.text self.to_s
else
pp.pp_object(self)
end
@@ -521,6 +523,23 @@ if __FILE__ == $0
a = proc {1}
assert_equal("#{a.inspect}\n", PP.pp(a, ''))
end
+
+ class Test_to_s_with_iv
+ def initialize() @a = nil end
+ def to_s() "aaa" end
+ end
+ def test_to_s_with_iv
+ a = Test_to_s_with_iv.new
+ assert_equal("#{a.inspect}\n", PP.pp(a, ''))
+ end
+
+ class Test_to_s_without_iv
+ def to_s() "aaa" end
+ end
+ def test_to_s_without_iv
+ a = Test_to_s_without_iv.new
+ assert_equal("#{a.inspect}\n", PP.pp(a, ''))
+ end
end
class PPCycleTest < Test::Unit::TestCase