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>
|
Wed Sep 11 18:55:38 2002 Nobuyoshi Nakada <nobu.nokada@softhome.net>
|
||||||
|
|
||||||
* eval.c (proc_to_s): refined format. [ruby-dev:18215]
|
* eval.c (proc_to_s): refined format. [ruby-dev:18215]
|
||||||
|
|
|
@ -507,7 +507,7 @@ class Context
|
||||||
end
|
end
|
||||||
|
|
||||||
when /^\s*pp\s+/
|
when /^\s*pp\s+/
|
||||||
PP.pp(debug_eval($', binding), 79, stdout)
|
PP.pp(debug_eval($', binding), stdout)
|
||||||
|
|
||||||
when /^\s*p\s+/
|
when /^\s*p\s+/
|
||||||
stdout.printf "%s\n", debug_eval($', binding).inspect
|
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>))
|
((<PrettyPrint>))
|
||||||
|
|
||||||
== class methods
|
== class methods
|
||||||
--- PP.pp(obj[, width[, out]])
|
--- PP.pp(obj[, out[, width]])
|
||||||
outputs ((|obj|)) to ((|out|)) in pretty printed format of
|
outputs ((|obj|)) to ((|out|)) in pretty printed format of
|
||||||
((|width|)) columns in width.
|
((|width|)) columns in width.
|
||||||
|
|
||||||
|
If ((|out|)) is ommitted, (({$>})) is assumed.
|
||||||
|
If ((|width|)) is ommitted, 79 is assumed.
|
||||||
|
|
||||||
|
PP.pp returns ((|out|)).
|
||||||
|
|
||||||
--- PP.sharing_detection
|
--- PP.sharing_detection
|
||||||
returns the sharing detection flag as boolean value.
|
returns the sharing detection flag as boolean value.
|
||||||
It is false by default.
|
It is false by default.
|
||||||
|
@ -116,8 +121,7 @@ PP#pp to print the object.
|
||||||
detected as part of a cycle.
|
detected as part of a cycle.
|
||||||
|
|
||||||
--- pretty_print_instance_variables
|
--- pretty_print_instance_variables
|
||||||
is a method to list instance variables used by the default implementation
|
returns a sorted array of instance variable names.
|
||||||
of (({pretty_print})).
|
|
||||||
|
|
||||||
This method should return an array of names of instance variables as symbols or strings as:
|
This method should return an array of names of instance variables as symbols or strings as:
|
||||||
(({[:@a, :@b]})).
|
(({[:@a, :@b]})).
|
||||||
|
@ -136,7 +140,7 @@ module Kernel
|
||||||
end
|
end
|
||||||
|
|
||||||
class PP < PrettyPrint
|
class PP < PrettyPrint
|
||||||
def PP.pp(obj, width=79, out=$>)
|
def PP.pp(obj, out=$>, width=79)
|
||||||
pp = PP.new(out, width)
|
pp = PP.new(out, width)
|
||||||
pp.guard_inspect_key {pp.pp obj}
|
pp.guard_inspect_key {pp.pp obj}
|
||||||
pp.flush
|
pp.flush
|
||||||
|
@ -434,6 +438,12 @@ class File
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class << ARGF
|
||||||
|
def pretty_print(pp)
|
||||||
|
pp.text self.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
include PP::ObjectMixin
|
include PP::ObjectMixin
|
||||||
end
|
end
|
||||||
|
@ -450,11 +460,11 @@ if __FILE__ == $0
|
||||||
|
|
||||||
class PPTest < RUNIT::TestCase
|
class PPTest < RUNIT::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))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_list0123_11
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -499,17 +509,17 @@ if __FILE__ == $0
|
||||||
class PPInspectTest < RUNIT::TestCase
|
class PPInspectTest < RUNIT::TestCase
|
||||||
def test_hasinspect
|
def test_hasinspect
|
||||||
a = HasInspect.new(1)
|
a = HasInspect.new(1)
|
||||||
assert_equal("<inspect:1>\n", PP.pp(a, 79, ''))
|
assert_equal("<inspect:1>\n", PP.pp(a, ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hasprettyprint
|
def test_hasprettyprint
|
||||||
a = HasPrettyPrint.new(1)
|
a = HasPrettyPrint.new(1)
|
||||||
assert_equal("<pretty_print:1>\n", PP.pp(a, 79, ''))
|
assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hasboth
|
def test_hasboth
|
||||||
a = HasBoth.new(1)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -517,32 +527,32 @@ if __FILE__ == $0
|
||||||
def test_array
|
def test_array
|
||||||
a = []
|
a = []
|
||||||
a << a
|
a << a
|
||||||
assert_equal("[[...]]\n", PP.pp(a, 79, ''))
|
assert_equal("[[...]]\n", PP.pp(a, ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hash
|
def test_hash
|
||||||
a = {}
|
a = {}
|
||||||
a[0] = a
|
a[0] = a
|
||||||
assert_equal("{0=>{...}}\n", PP.pp(a, 79, ''))
|
assert_equal("{0=>{...}}\n", PP.pp(a, ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
S = Struct.new("S", :a, :b)
|
S = Struct.new("S", :a, :b)
|
||||||
def test_struct
|
def test_struct
|
||||||
a = S.new(1,2)
|
a = S.new(1,2)
|
||||||
a.b = a
|
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
|
end
|
||||||
|
|
||||||
def test_object
|
def test_object
|
||||||
a = Object.new
|
a = Object.new
|
||||||
a.instance_eval {@a = a}
|
a.instance_eval {@a = a}
|
||||||
assert_equal(a.inspect + "\n", PP.pp(a, 79, ''))
|
assert_equal(a.inspect + "\n", PP.pp(a, ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_withinspect
|
def test_withinspect
|
||||||
a = []
|
a = []
|
||||||
a << HasInspect.new(a)
|
a << HasInspect.new(a)
|
||||||
assert_equal("[<inspect:[...]>]\n", PP.pp(a, 79, ''))
|
assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -421,7 +421,7 @@ class Set
|
||||||
pp.text "}>"
|
pp.text "}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
def pretty_print_cycled(pp)
|
def pretty_print_cycle(pp)
|
||||||
pp.text sprintf('#<%s: {%s}>', type.name, empty? ? '' : '...')
|
pp.text sprintf('#<%s: {%s}>', type.name, empty? ? '' : '...')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -820,7 +820,7 @@ class TC_Set < Test::Unit::TestCase
|
||||||
# def test_pretty_print
|
# def test_pretty_print
|
||||||
# end
|
# end
|
||||||
|
|
||||||
# def test_pretty_print_cycled
|
# def test_pretty_print_cycle
|
||||||
# end
|
# end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -78,9 +78,13 @@ TSort uses Hash internally.
|
||||||
--- tsort_each_node {|node| ...}
|
--- tsort_each_node {|node| ...}
|
||||||
should be implemented by a extended class.
|
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| ...}
|
--- tsort_each_child(node) {|child| ...}
|
||||||
should be implemented by a extended class.
|
should be implemented by a extended class.
|
||||||
|
|
||||||
|
(({tsort_each_child})) is used to iterate for child nodes of ((|node|)).
|
||||||
|
|
||||||
== More Realistic Example
|
== More Realistic Example
|
||||||
Very simple `make' like tool can be implemented as follows:
|
Very simple `make' like tool can be implemented as follows:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue