From c47f24027a3473fbdf9510cc300565fb1e6f1db8 Mon Sep 17 00:00:00 2001 From: akr Date: Wed, 11 Sep 2002 12:29:17 +0000 Subject: * pp.rb (ARGF.pretty_print): implemented. (PP.pp): arguments reordered. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2851 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/debug.rb | 2 +- lib/pp.rb | 38 ++++++++++++++++++++++++-------------- lib/set.rb | 4 ++-- lib/tsort.rb | 4 ++++ 4 files changed, 31 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/debug.rb b/lib/debug.rb index 071840d65e..b6625848b3 100644 --- a/lib/debug.rb +++ b/lib/debug.rb @@ -507,7 +507,7 @@ class Context end when /^\s*pp\s+/ - PP.pp(debug_eval($', binding), 79, stdout) + PP.pp(debug_eval($', binding), stdout) when /^\s*p\s+/ stdout.printf "%s\n", debug_eval($', binding).inspect diff --git a/lib/pp.rb b/lib/pp.rb index 81b06df003..0eb8f28ac6 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -69,10 +69,15 @@ PP#pp to print the object. (()) == class methods ---- PP.pp(obj[, width[, out]]) +--- PP.pp(obj[, out[, width]]) outputs ((|obj|)) to ((|out|)) in pretty printed format of ((|width|)) columns in width. + If ((|out|)) is ommitted, (({$>})) is assumed. + If ((|width|)) is ommitted, 79 is assumed. + + PP.pp returns ((|out|)). + --- PP.sharing_detection returns the sharing detection flag as boolean value. It is false by default. @@ -116,8 +121,7 @@ PP#pp to print the object. detected as part of a cycle. --- pretty_print_instance_variables - is a method to list instance variables used by the default implementation - of (({pretty_print})). + returns a sorted array of instance variable names. This method should return an array of names of instance variables as symbols or strings as: (({[:@a, :@b]})). @@ -136,7 +140,7 @@ module Kernel end class PP < PrettyPrint - def PP.pp(obj, width=79, out=$>) + def PP.pp(obj, out=$>, width=79) pp = PP.new(out, width) pp.guard_inspect_key {pp.pp obj} pp.flush @@ -434,6 +438,12 @@ class File end end +class << ARGF + def pretty_print(pp) + pp.text self.to_s + end +end + class Object include PP::ObjectMixin end @@ -450,11 +460,11 @@ if __FILE__ == $0 class PPTest < RUNIT::TestCase 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)) end def test_list0123_11 - assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], 11, '')) + assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11)) end end @@ -499,17 +509,17 @@ if __FILE__ == $0 class PPInspectTest < RUNIT::TestCase def test_hasinspect a = HasInspect.new(1) - assert_equal("\n", PP.pp(a, 79, '')) + assert_equal("\n", PP.pp(a, '')) end def test_hasprettyprint a = HasPrettyPrint.new(1) - assert_equal("\n", PP.pp(a, 79, '')) + assert_equal("\n", PP.pp(a, '')) end def test_hasboth a = HasBoth.new(1) - assert_equal("\n", PP.pp(a, 79, '')) + assert_equal("\n", PP.pp(a, '')) end end @@ -517,32 +527,32 @@ if __FILE__ == $0 def test_array a = [] a << a - assert_equal("[[...]]\n", PP.pp(a, 79, '')) + assert_equal("[[...]]\n", PP.pp(a, '')) end def test_hash a = {} a[0] = a - assert_equal("{0=>{...}}\n", PP.pp(a, 79, '')) + assert_equal("{0=>{...}}\n", PP.pp(a, '')) end S = Struct.new("S", :a, :b) def test_struct a = S.new(1,2) a.b = a - assert_equal("#>\n", PP.pp(a, 79, '')) + assert_equal("#>\n", PP.pp(a, '')) end def test_object a = Object.new a.instance_eval {@a = a} - assert_equal(a.inspect + "\n", PP.pp(a, 79, '')) + assert_equal(a.inspect + "\n", PP.pp(a, '')) end def test_withinspect a = [] a << HasInspect.new(a) - assert_equal("[]\n", PP.pp(a, 79, '')) + assert_equal("[]\n", PP.pp(a, '')) end end diff --git a/lib/set.rb b/lib/set.rb index edd219b34d..f55d1b7f30 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -421,7 +421,7 @@ class Set pp.text "}>" end - def pretty_print_cycled(pp) + def pretty_print_cycle(pp) pp.text sprintf('#<%s: {%s}>', type.name, empty? ? '' : '...') end end @@ -820,7 +820,7 @@ class TC_Set < Test::Unit::TestCase # def test_pretty_print # end - # def test_pretty_print_cycled + # def test_pretty_print_cycle # end end diff --git a/lib/tsort.rb b/lib/tsort.rb index 0dd68c6b8a..21adeaabfe 100644 --- a/lib/tsort.rb +++ b/lib/tsort.rb @@ -78,9 +78,13 @@ TSort uses Hash internally. --- tsort_each_node {|node| ...} should be implemented by a extended class. + (({tsort_each_node})) is used to iterate for all nodes over a graph. + --- tsort_each_child(node) {|child| ...} should be implemented by a extended class. + (({tsort_each_child})) is used to iterate for child nodes of ((|node|)). + == More Realistic Example Very simple `make' like tool can be implemented as follows: -- cgit v1.2.3