mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
bfb1775244
commit
c47f24027a
5 changed files with 36 additions and 17 deletions
|
@ -1,3 +1,8 @@
|
|||
Wed Sep 11 21:25:52 2002 Tanaka Akira <akr@m17n.org>
|
||||
|
||||
* pp.rb (ARGF.pretty_print): implemented.
|
||||
(PP.pp): arguments reordered.
|
||||
|
||||
Wed Sep 11 18:55:38 2002 Nobuyoshi Nakada <nobu.nokada@softhome.net>
|
||||
|
||||
* eval.c (proc_to_s): refined format. [ruby-dev:18215]
|
||||
|
|
|
@ -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
|
||||
|
|
38
lib/pp.rb
38
lib/pp.rb
|
@ -69,10 +69,15 @@ PP#pp to print the object.
|
|||
((<PrettyPrint>))
|
||||
|
||||
== 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("<inspect:1>\n", PP.pp(a, 79, ''))
|
||||
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, 79, ''))
|
||||
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, 79, ''))
|
||||
assert_equal("<pretty_print:1>\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("#<Struct::S a=1, b=#<Struct::S:...>>\n", PP.pp(a, 79, ''))
|
||||
assert_equal("#<Struct::S a=1, b=#<Struct::S:...>>\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("[<inspect:[...]>]\n", PP.pp(a, 79, ''))
|
||||
assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue