* lib/pp.rb: don't use local variable `pp'.

* lib/prettyprint.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5200 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2003-12-16 12:22:15 +00:00
parent c5bbcadbe6
commit ff9f067f89
3 changed files with 194 additions and 188 deletions

View File

@ -1,3 +1,9 @@
Tue Dec 16 21:20:47 2003 Tanaka Akira <akr@m17n.org>
* lib/pp.rb: don't use local variable `pp'.
* lib/prettyprint.rb: ditto.
Tue Dec 16 13:20:43 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> Tue Dec 16 13:20:43 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/lib/tk.rb: condition bug of if statement on * ext/tk/lib/tk.rb: condition bug of if statement on

194
lib/pp.rb
View File

@ -135,17 +135,17 @@ end
class PP < PrettyPrint class PP < PrettyPrint
def PP.pp(obj, out=$>, width=79) def PP.pp(obj, out=$>, width=79)
pp = PP.new(out, width) q = PP.new(out, width)
pp.guard_inspect_key {pp.pp obj} q.guard_inspect_key {q.pp obj}
pp.flush q.flush
#$pp = pp #$pp = q
out << "\n" out << "\n"
end end
def PP.singleline_pp(obj, out=$>) def PP.singleline_pp(obj, out=$>)
pp = SingleLine.new(out) q = SingleLine.new(out)
pp.guard_inspect_key {pp.pp obj} q.guard_inspect_key {q.pp obj}
pp.flush q.flush
out out
end end
@ -246,20 +246,20 @@ class PP < PrettyPrint
# 3. specific to_s if instance variable is empty # 3. specific to_s if instance variable is empty
# 4. generic pretty_print # 4. generic pretty_print
def pretty_print(pp) def pretty_print(q)
if /\(Kernel\)#/ !~ method(:inspect).inspect if /\(Kernel\)#/ !~ method(:inspect).inspect
pp.text self.inspect q.text self.inspect
elsif /\(Kernel\)#/ !~ method(:to_s).inspect && instance_variables.empty? elsif /\(Kernel\)#/ !~ method(:to_s).inspect && instance_variables.empty?
pp.text self.to_s q.text self.to_s
else else
pp.pp_object(self) q.pp_object(self)
end end
end end
def pretty_print_cycle(pp) def pretty_print_cycle(q)
pp.object_address_group(self) { q.object_address_group(self) {
pp.breakable q.breakable
pp.text '...' q.text '...'
} }
end end
@ -277,80 +277,80 @@ class PP < PrettyPrint
end end
class Array class Array
def pretty_print(pp) def pretty_print(q)
pp.group(1, '[', ']') { q.group(1, '[', ']') {
self.each {|v| self.each {|v|
pp.comma_breakable unless pp.first? q.comma_breakable unless q.first?
pp.pp v q.pp v
} }
} }
end end
def pretty_print_cycle(pp) def pretty_print_cycle(q)
pp.text(empty? ? '[]' : '[...]') q.text(empty? ? '[]' : '[...]')
end end
end end
class Hash class Hash
def pretty_print(pp) def pretty_print(q)
pp.pp_hash self q.pp_hash self
end end
def pretty_print_cycle(pp) def pretty_print_cycle(q)
pp.text(empty? ? '{}' : '{...}') q.text(empty? ? '{}' : '{...}')
end end
end end
class << ENV class << ENV
def pretty_print(pp) def pretty_print(q)
pp.pp_hash self q.pp_hash self
end end
end end
class Struct class Struct
def pretty_print(pp) def pretty_print(q)
pp.object_group(self) { q.object_group(self) {
self.members.each {|member| self.members.each {|member|
pp.text "," unless pp.first? q.text "," unless q.first?
pp.breakable q.breakable
pp.text member.to_s q.text member.to_s
pp.text '=' q.text '='
pp.group(1) { q.group(1) {
pp.breakable '' q.breakable ''
pp.pp self[member] q.pp self[member]
} }
} }
} }
end end
def pretty_print_cycle(pp) def pretty_print_cycle(q)
pp.text sprintf("#<%s:...>", self.class.name) q.text sprintf("#<%s:...>", self.class.name)
end end
end end
class Range class Range
def pretty_print(pp) def pretty_print(q)
pp.pp self.begin q.pp self.begin
pp.breakable '' q.breakable ''
pp.text(self.exclude_end? ? '...' : '..') q.text(self.exclude_end? ? '...' : '..')
pp.breakable '' q.breakable ''
pp.pp self.end q.pp self.end
end end
end end
class File class File
class Stat class Stat
def pretty_print(pp) def pretty_print(q)
require 'etc.so' require 'etc.so'
pp.object_group(self) { q.object_group(self) {
pp.breakable q.breakable
pp.text sprintf("dev=0x%x", self.dev); pp.comma_breakable q.text sprintf("dev=0x%x", self.dev); q.comma_breakable
pp.text "ino="; pp.pp self.ino; pp.comma_breakable q.text "ino="; q.pp self.ino; q.comma_breakable
pp.group { q.group {
m = self.mode m = self.mode
pp.text sprintf("mode=0%o", m) q.text sprintf("mode=0%o", m)
pp.breakable q.breakable
pp.text sprintf("(%s %c%c%c%c%c%c%c%c%c)", q.text sprintf("(%s %c%c%c%c%c%c%c%c%c)",
self.ftype, self.ftype,
(m & 0400 == 0 ? ?- : ?r), (m & 0400 == 0 ? ?- : ?r),
(m & 0200 == 0 ? ?- : ?w), (m & 0200 == 0 ? ?- : ?w),
@ -365,51 +365,51 @@ class File
(m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) : (m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
(m & 01000 == 0 ? ?x : ?t))) (m & 01000 == 0 ? ?x : ?t)))
} }
pp.comma_breakable q.comma_breakable
pp.text "nlink="; pp.pp self.nlink; pp.comma_breakable q.text "nlink="; q.pp self.nlink; q.comma_breakable
pp.group { q.group {
pp.text "uid="; pp.pp self.uid q.text "uid="; q.pp self.uid
begin begin
name = Etc.getpwuid(self.uid).name name = Etc.getpwuid(self.uid).name
pp.breakable; pp.text "(#{name})" q.breakable; q.text "(#{name})"
rescue ArgumentError rescue ArgumentError
end end
} }
pp.comma_breakable q.comma_breakable
pp.group { q.group {
pp.text "gid="; pp.pp self.gid q.text "gid="; q.pp self.gid
begin begin
name = Etc.getgrgid(self.gid).name name = Etc.getgrgid(self.gid).name
pp.breakable; pp.text "(#{name})" q.breakable; q.text "(#{name})"
rescue ArgumentError rescue ArgumentError
end end
} }
pp.comma_breakable q.comma_breakable
pp.group { q.group {
pp.text sprintf("rdev=0x%x", self.rdev) q.text sprintf("rdev=0x%x", self.rdev)
pp.breakable q.breakable
pp.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor) q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor)
} }
pp.comma_breakable q.comma_breakable
pp.text "size="; pp.pp self.size; pp.comma_breakable q.text "size="; q.pp self.size; q.comma_breakable
pp.text "blksize="; pp.pp self.blksize; pp.comma_breakable q.text "blksize="; q.pp self.blksize; q.comma_breakable
pp.text "blocks="; pp.pp self.blocks; pp.comma_breakable q.text "blocks="; q.pp self.blocks; q.comma_breakable
pp.group { q.group {
t = self.atime t = self.atime
pp.text "atime="; pp.pp t q.text "atime="; q.pp t
pp.breakable; pp.text "(#{t.tv_sec})" q.breakable; q.text "(#{t.tv_sec})"
} }
pp.comma_breakable q.comma_breakable
pp.group { q.group {
t = self.mtime t = self.mtime
pp.text "mtime="; pp.pp t q.text "mtime="; q.pp t
pp.breakable; pp.text "(#{t.tv_sec})" q.breakable; q.text "(#{t.tv_sec})"
} }
pp.comma_breakable q.comma_breakable
pp.group { q.group {
t = self.ctime t = self.ctime
pp.text "ctime="; pp.pp t q.text "ctime="; q.pp t
pp.breakable; pp.text "(#{t.tv_sec})" q.breakable; q.text "(#{t.tv_sec})"
} }
} }
end end
@ -417,12 +417,12 @@ class File
end end
class MatchData class MatchData
def pretty_print(pp) def pretty_print(q)
pp.object_group(self) { q.object_group(self) {
pp.breakable q.breakable
1.upto(self.size) {|i| 1.upto(self.size) {|i|
pp.breakable unless pp.first? q.breakable unless q.first?
pp.pp self[i-1] q.pp self[i-1]
} }
} }
end end
@ -434,8 +434,8 @@ end
[Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c| [Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c|
c.class_eval { c.class_eval {
def pretty_print_cycle(pp) def pretty_print_cycle(q)
pp.text inspect q.text inspect
end end
} }
} }
@ -468,10 +468,10 @@ if __FILE__ == $0
@a = a @a = a
end end
def pretty_print(pp) def pretty_print(q)
pp.text "<pretty_print:" q.text "<pretty_print:"
pp.pp @a q.pp @a
pp.text ">" q.text ">"
end end
end end
@ -484,10 +484,10 @@ if __FILE__ == $0
return "<inspect:#{@a.inspect}>" return "<inspect:#{@a.inspect}>"
end end
def pretty_print(pp) def pretty_print(q)
pp.text "<pretty_print:" q.text "<pretty_print:"
pp.pp @a q.pp @a
pp.text ">" q.text ">"
end end
end end

View File

@ -41,17 +41,17 @@ non-string formatting, etc.
The block is used to generate spaces. The block is used to generate spaces.
(({{|width| ' ' * width}})) is used if it is not given. (({{|width| ' ' * width}})) is used if it is not given.
--- PrettyPrint.format([output[, maxwidth[, newline[, genspace]]]]) {|pp| ...} --- PrettyPrint.format([output[, maxwidth[, newline[, genspace]]]]) {|q| ...}
is a convenience method which is same as follows: is a convenience method which is same as follows:
begin begin
pp = PrettyPrint.new(output, maxwidth, newline, &genspace) q = PrettyPrint.new(output, maxwidth, newline, &genspace)
... ...
pp.flush q.flush
output output
end end
--- PrettyPrint.singleline_format([output[, maxwidth[, newline[, genspace]]]]) {|pp| ...} --- PrettyPrint.singleline_format([output[, maxwidth[, newline[, genspace]]]]) {|q| ...}
is similar to (({PrettyPrint.format})) but the result has no breaks. is similar to (({PrettyPrint.format})) but the result has no breaks.
((|maxwidth|)), ((|newline|)) and ((|genspace|)) are ignored. ((|maxwidth|)), ((|newline|)) and ((|genspace|)) are ignored.
@ -99,11 +99,11 @@ non-string formatting, etc.
current group. current group.
It is useful to format comma separated values as: It is useful to format comma separated values as:
pp.group(1, '[', ']') { q.group(1, '[', ']') {
xxx.each {|yyy| xxx.each {|yyy|
unless pp.first? unless q.first?
pp.text ',' q.text ','
pp.breakable q.breakable
end end
... pretty printing yyy ... ... pretty printing yyy ...
} }
@ -125,15 +125,15 @@ Tanaka Akira <akr@m17n.org>
class PrettyPrint class PrettyPrint
def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n}) def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
pp = PrettyPrint.new(output, maxwidth, newline, &genspace) q = PrettyPrint.new(output, maxwidth, newline, &genspace)
yield pp yield q
pp.flush q.flush
output output
end end
def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil) def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
pp = SingleLine.new(output) q = SingleLine.new(output)
yield pp yield q
output output
end end
@ -274,12 +274,12 @@ class PrettyPrint
end end
class Breakable class Breakable
def initialize(sep, width, pp) def initialize(sep, width, q)
@obj = sep @obj = sep
@width = width @width = width
@pp = pp @pp = q
@indent = pp.indent @indent = q.indent
@group = pp.current_group @group = q.current_group
@group.breakables.push self @group.breakables.push self
end end
attr_reader :obj, :width, :indent attr_reader :obj, :width, :indent
@ -474,7 +474,7 @@ End
end end
def tree(width) def tree(width)
PrettyPrint.format('', width) {|pp| @tree.show(pp)} PrettyPrint.format('', width) {|q| @tree.show(q)}
end end
def test_tree_00_19 def test_tree_00_19
@ -519,7 +519,7 @@ End
end end
def tree_alt(width) def tree_alt(width)
PrettyPrint.format('', width) {|pp| @tree.altshow(pp)} PrettyPrint.format('', width) {|q| @tree.altshow(q)}
end end
def test_tree_alt_00_18 def test_tree_alt_00_18
@ -582,50 +582,50 @@ End
@children = children @children = children
end end
def show(pp) def show(q)
pp.group { q.group {
pp.text @string q.text @string
pp.nest(@string.length) { q.nest(@string.length) {
unless @children.empty? unless @children.empty?
pp.text '[' q.text '['
pp.nest(1) { q.nest(1) {
first = true first = true
@children.each {|t| @children.each {|t|
if first if first
first = false first = false
else else
pp.text ',' q.text ','
pp.breakable q.breakable
end end
t.show(pp) t.show(q)
} }
} }
pp.text ']' q.text ']'
end end
} }
} }
end end
def altshow(pp) def altshow(q)
pp.group { q.group {
pp.text @string q.text @string
unless @children.empty? unless @children.empty?
pp.text '[' q.text '['
pp.nest(2) { q.nest(2) {
pp.breakable q.breakable
first = true first = true
@children.each {|t| @children.each {|t|
if first if first
first = false first = false
else else
pp.text ',' q.text ','
pp.breakable q.breakable
end end
t.altshow(pp) t.altshow(q)
} }
} }
pp.breakable q.breakable
pp.text ']' q.text ']'
end end
} }
end end
@ -635,28 +635,28 @@ End
class StrictPrettyExample < Test::Unit::TestCase class StrictPrettyExample < Test::Unit::TestCase
def prog(width) def prog(width)
PrettyPrint.format('', width) {|pp| PrettyPrint.format('', width) {|q|
pp.group { q.group {
pp.group {pp.nest(2) { q.group {q.nest(2) {
pp.text "if"; pp.breakable; q.text "if"; q.breakable;
pp.group { q.group {
pp.nest(2) { q.nest(2) {
pp.group {pp.text "a"; pp.breakable; pp.text "=="} q.group {q.text "a"; q.breakable; q.text "=="}
pp.breakable; pp.text "b"}}}} q.breakable; q.text "b"}}}}
pp.breakable q.breakable
pp.group {pp.nest(2) { q.group {q.nest(2) {
pp.text "then"; pp.breakable; q.text "then"; q.breakable;
pp.group { q.group {
pp.nest(2) { q.nest(2) {
pp.group {pp.text "a"; pp.breakable; pp.text "<<"} q.group {q.text "a"; q.breakable; q.text "<<"}
pp.breakable; pp.text "2"}}}} q.breakable; q.text "2"}}}}
pp.breakable q.breakable
pp.group {pp.nest(2) { q.group {q.nest(2) {
pp.text "else"; pp.breakable; q.text "else"; q.breakable;
pp.group { q.group {
pp.nest(2) { q.nest(2) {
pp.group {pp.text "a"; pp.breakable; pp.text "+"} q.group {q.text "a"; q.breakable; q.text "+"}
pp.breakable; pp.text "b"}}}}} q.breakable; q.text "b"}}}}}
} }
end end
@ -780,17 +780,17 @@ End
class TailGroup < Test::Unit::TestCase class TailGroup < Test::Unit::TestCase
def test_1 def test_1
out = PrettyPrint.format('', 10) {|pp| out = PrettyPrint.format('', 10) {|q|
pp.group { q.group {
pp.group { q.group {
pp.text "abc" q.text "abc"
pp.breakable q.breakable
pp.text "def" q.text "def"
} }
pp.group { q.group {
pp.text "ghi" q.text "ghi"
pp.breakable q.breakable
pp.text "jkl" q.text "jkl"
} }
} }
} }
@ -800,10 +800,10 @@ End
class NonString < Test::Unit::TestCase class NonString < Test::Unit::TestCase
def format(width) def format(width)
PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|pp| PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q|
pp.text(3, 3) q.text(3, 3)
pp.breakable(1, 1) q.breakable(1, 1)
pp.text(3, 3) q.text(3, 3)
} }
end end
@ -819,21 +819,21 @@ End
class Fill < Test::Unit::TestCase class Fill < Test::Unit::TestCase
def format(width) def format(width)
PrettyPrint.format('', width) {|pp| PrettyPrint.format('', width) {|q|
pp.group { q.group {
pp.text 'abc' q.text 'abc'
pp.fill_breakable q.fill_breakable
pp.text 'def' q.text 'def'
pp.fill_breakable q.fill_breakable
pp.text 'ghi' q.text 'ghi'
pp.fill_breakable q.fill_breakable
pp.text 'jkl' q.text 'jkl'
pp.fill_breakable q.fill_breakable
pp.text 'mno' q.text 'mno'
pp.fill_breakable q.fill_breakable
pp.text 'pqr' q.text 'pqr'
pp.fill_breakable q.fill_breakable
pp.text 'stu' q.text 'stu'
} }
} }
end end