From ff9f067f8977c64f1a09e676a28ae1043b932a51 Mon Sep 17 00:00:00 2001 From: akr Date: Tue, 16 Dec 2003 12:22:15 +0000 Subject: * 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 --- lib/pp.rb | 194 ++++++++++++++++++++++++++--------------------------- lib/prettyprint.rb | 182 ++++++++++++++++++++++++------------------------- 2 files changed, 188 insertions(+), 188 deletions(-) (limited to 'lib') diff --git a/lib/pp.rb b/lib/pp.rb index 9babc1e7a9..03d9248218 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -135,17 +135,17 @@ end class PP < PrettyPrint def PP.pp(obj, out=$>, width=79) - pp = PP.new(out, width) - pp.guard_inspect_key {pp.pp obj} - pp.flush - #$pp = pp + q = PP.new(out, width) + q.guard_inspect_key {q.pp obj} + q.flush + #$pp = q out << "\n" end def PP.singleline_pp(obj, out=$>) - pp = SingleLine.new(out) - pp.guard_inspect_key {pp.pp obj} - pp.flush + q = SingleLine.new(out) + q.guard_inspect_key {q.pp obj} + q.flush out end @@ -246,20 +246,20 @@ class PP < PrettyPrint # 3. specific to_s if instance variable is empty # 4. generic pretty_print - def pretty_print(pp) + def pretty_print(q) if /\(Kernel\)#/ !~ method(:inspect).inspect - pp.text self.inspect + q.text self.inspect elsif /\(Kernel\)#/ !~ method(:to_s).inspect && instance_variables.empty? - pp.text self.to_s + q.text self.to_s else - pp.pp_object(self) + q.pp_object(self) end end - def pretty_print_cycle(pp) - pp.object_address_group(self) { - pp.breakable - pp.text '...' + def pretty_print_cycle(q) + q.object_address_group(self) { + q.breakable + q.text '...' } end @@ -277,80 +277,80 @@ class PP < PrettyPrint end class Array - def pretty_print(pp) - pp.group(1, '[', ']') { + def pretty_print(q) + q.group(1, '[', ']') { self.each {|v| - pp.comma_breakable unless pp.first? - pp.pp v + q.comma_breakable unless q.first? + q.pp v } } end - def pretty_print_cycle(pp) - pp.text(empty? ? '[]' : '[...]') + def pretty_print_cycle(q) + q.text(empty? ? '[]' : '[...]') end end class Hash - def pretty_print(pp) - pp.pp_hash self + def pretty_print(q) + q.pp_hash self end - def pretty_print_cycle(pp) - pp.text(empty? ? '{}' : '{...}') + def pretty_print_cycle(q) + q.text(empty? ? '{}' : '{...}') end end class << ENV - def pretty_print(pp) - pp.pp_hash self + def pretty_print(q) + q.pp_hash self end end class Struct - def pretty_print(pp) - pp.object_group(self) { + def pretty_print(q) + q.object_group(self) { self.members.each {|member| - pp.text "," unless pp.first? - pp.breakable - pp.text member.to_s - pp.text '=' - pp.group(1) { - pp.breakable '' - pp.pp self[member] + q.text "," unless q.first? + q.breakable + q.text member.to_s + q.text '=' + q.group(1) { + q.breakable '' + q.pp self[member] } } } end - def pretty_print_cycle(pp) - pp.text sprintf("#<%s:...>", self.class.name) + def pretty_print_cycle(q) + q.text sprintf("#<%s:...>", self.class.name) end end class Range - def pretty_print(pp) - pp.pp self.begin - pp.breakable '' - pp.text(self.exclude_end? ? '...' : '..') - pp.breakable '' - pp.pp self.end + def pretty_print(q) + q.pp self.begin + q.breakable '' + q.text(self.exclude_end? ? '...' : '..') + q.breakable '' + q.pp self.end end end class File class Stat - def pretty_print(pp) + def pretty_print(q) require 'etc.so' - pp.object_group(self) { - pp.breakable - pp.text sprintf("dev=0x%x", self.dev); pp.comma_breakable - pp.text "ino="; pp.pp self.ino; pp.comma_breakable - pp.group { + q.object_group(self) { + q.breakable + q.text sprintf("dev=0x%x", self.dev); q.comma_breakable + q.text "ino="; q.pp self.ino; q.comma_breakable + q.group { m = self.mode - pp.text sprintf("mode=0%o", m) - pp.breakable - pp.text sprintf("(%s %c%c%c%c%c%c%c%c%c)", + q.text sprintf("mode=0%o", m) + q.breakable + q.text sprintf("(%s %c%c%c%c%c%c%c%c%c)", self.ftype, (m & 0400 == 0 ? ?- : ?r), (m & 0200 == 0 ? ?- : ?w), @@ -365,51 +365,51 @@ class File (m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) : (m & 01000 == 0 ? ?x : ?t))) } - pp.comma_breakable - pp.text "nlink="; pp.pp self.nlink; pp.comma_breakable - pp.group { - pp.text "uid="; pp.pp self.uid + q.comma_breakable + q.text "nlink="; q.pp self.nlink; q.comma_breakable + q.group { + q.text "uid="; q.pp self.uid begin name = Etc.getpwuid(self.uid).name - pp.breakable; pp.text "(#{name})" + q.breakable; q.text "(#{name})" rescue ArgumentError end } - pp.comma_breakable - pp.group { - pp.text "gid="; pp.pp self.gid + q.comma_breakable + q.group { + q.text "gid="; q.pp self.gid begin name = Etc.getgrgid(self.gid).name - pp.breakable; pp.text "(#{name})" + q.breakable; q.text "(#{name})" rescue ArgumentError end } - pp.comma_breakable - pp.group { - pp.text sprintf("rdev=0x%x", self.rdev) - pp.breakable - pp.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor) + q.comma_breakable + q.group { + q.text sprintf("rdev=0x%x", self.rdev) + q.breakable + q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor) } - pp.comma_breakable - pp.text "size="; pp.pp self.size; pp.comma_breakable - pp.text "blksize="; pp.pp self.blksize; pp.comma_breakable - pp.text "blocks="; pp.pp self.blocks; pp.comma_breakable - pp.group { + q.comma_breakable + q.text "size="; q.pp self.size; q.comma_breakable + q.text "blksize="; q.pp self.blksize; q.comma_breakable + q.text "blocks="; q.pp self.blocks; q.comma_breakable + q.group { t = self.atime - pp.text "atime="; pp.pp t - pp.breakable; pp.text "(#{t.tv_sec})" + q.text "atime="; q.pp t + q.breakable; q.text "(#{t.tv_sec})" } - pp.comma_breakable - pp.group { + q.comma_breakable + q.group { t = self.mtime - pp.text "mtime="; pp.pp t - pp.breakable; pp.text "(#{t.tv_sec})" + q.text "mtime="; q.pp t + q.breakable; q.text "(#{t.tv_sec})" } - pp.comma_breakable - pp.group { + q.comma_breakable + q.group { t = self.ctime - pp.text "ctime="; pp.pp t - pp.breakable; pp.text "(#{t.tv_sec})" + q.text "ctime="; q.pp t + q.breakable; q.text "(#{t.tv_sec})" } } end @@ -417,12 +417,12 @@ class File end class MatchData - def pretty_print(pp) - pp.object_group(self) { - pp.breakable + def pretty_print(q) + q.object_group(self) { + q.breakable 1.upto(self.size) {|i| - pp.breakable unless pp.first? - pp.pp self[i-1] + q.breakable unless q.first? + q.pp self[i-1] } } end @@ -434,8 +434,8 @@ end [Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c| c.class_eval { - def pretty_print_cycle(pp) - pp.text inspect + def pretty_print_cycle(q) + q.text inspect end } } @@ -468,10 +468,10 @@ if __FILE__ == $0 @a = a end - def pretty_print(pp) - pp.text "" + def pretty_print(q) + q.text "" end end @@ -484,10 +484,10 @@ if __FILE__ == $0 return "" end - def pretty_print(pp) - pp.text "" + def pretty_print(q) + q.text "" end end diff --git a/lib/prettyprint.rb b/lib/prettyprint.rb index a815f64538..0aa54e91f9 100644 --- a/lib/prettyprint.rb +++ b/lib/prettyprint.rb @@ -41,17 +41,17 @@ non-string formatting, etc. The block is used to generate spaces. (({{|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: begin - pp = PrettyPrint.new(output, maxwidth, newline, &genspace) + q = PrettyPrint.new(output, maxwidth, newline, &genspace) ... - pp.flush + q.flush output 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. ((|maxwidth|)), ((|newline|)) and ((|genspace|)) are ignored. @@ -99,11 +99,11 @@ non-string formatting, etc. current group. It is useful to format comma separated values as: - pp.group(1, '[', ']') { + q.group(1, '[', ']') { xxx.each {|yyy| - unless pp.first? - pp.text ',' - pp.breakable + unless q.first? + q.text ',' + q.breakable end ... pretty printing yyy ... } @@ -125,15 +125,15 @@ Tanaka Akira class PrettyPrint def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n}) - pp = PrettyPrint.new(output, maxwidth, newline, &genspace) - yield pp - pp.flush + q = PrettyPrint.new(output, maxwidth, newline, &genspace) + yield q + q.flush output end def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil) - pp = SingleLine.new(output) - yield pp + q = SingleLine.new(output) + yield q output end @@ -274,12 +274,12 @@ class PrettyPrint end class Breakable - def initialize(sep, width, pp) + def initialize(sep, width, q) @obj = sep @width = width - @pp = pp - @indent = pp.indent - @group = pp.current_group + @pp = q + @indent = q.indent + @group = q.current_group @group.breakables.push self end attr_reader :obj, :width, :indent @@ -474,7 +474,7 @@ End end def tree(width) - PrettyPrint.format('', width) {|pp| @tree.show(pp)} + PrettyPrint.format('', width) {|q| @tree.show(q)} end def test_tree_00_19 @@ -519,7 +519,7 @@ End end def tree_alt(width) - PrettyPrint.format('', width) {|pp| @tree.altshow(pp)} + PrettyPrint.format('', width) {|q| @tree.altshow(q)} end def test_tree_alt_00_18 @@ -582,50 +582,50 @@ End @children = children end - def show(pp) - pp.group { - pp.text @string - pp.nest(@string.length) { + def show(q) + q.group { + q.text @string + q.nest(@string.length) { unless @children.empty? - pp.text '[' - pp.nest(1) { + q.text '[' + q.nest(1) { first = true @children.each {|t| if first first = false else - pp.text ',' - pp.breakable + q.text ',' + q.breakable end - t.show(pp) + t.show(q) } } - pp.text ']' + q.text ']' end } } end - def altshow(pp) - pp.group { - pp.text @string + def altshow(q) + q.group { + q.text @string unless @children.empty? - pp.text '[' - pp.nest(2) { - pp.breakable + q.text '[' + q.nest(2) { + q.breakable first = true @children.each {|t| if first first = false else - pp.text ',' - pp.breakable + q.text ',' + q.breakable end - t.altshow(pp) + t.altshow(q) } } - pp.breakable - pp.text ']' + q.breakable + q.text ']' end } end @@ -635,28 +635,28 @@ End class StrictPrettyExample < Test::Unit::TestCase def prog(width) - PrettyPrint.format('', width) {|pp| - pp.group { - pp.group {pp.nest(2) { - pp.text "if"; pp.breakable; - pp.group { - pp.nest(2) { - pp.group {pp.text "a"; pp.breakable; pp.text "=="} - pp.breakable; pp.text "b"}}}} - pp.breakable - pp.group {pp.nest(2) { - pp.text "then"; pp.breakable; - pp.group { - pp.nest(2) { - pp.group {pp.text "a"; pp.breakable; pp.text "<<"} - pp.breakable; pp.text "2"}}}} - pp.breakable - pp.group {pp.nest(2) { - pp.text "else"; pp.breakable; - pp.group { - pp.nest(2) { - pp.group {pp.text "a"; pp.breakable; pp.text "+"} - pp.breakable; pp.text "b"}}}}} + PrettyPrint.format('', width) {|q| + q.group { + q.group {q.nest(2) { + q.text "if"; q.breakable; + q.group { + q.nest(2) { + q.group {q.text "a"; q.breakable; q.text "=="} + q.breakable; q.text "b"}}}} + q.breakable + q.group {q.nest(2) { + q.text "then"; q.breakable; + q.group { + q.nest(2) { + q.group {q.text "a"; q.breakable; q.text "<<"} + q.breakable; q.text "2"}}}} + q.breakable + q.group {q.nest(2) { + q.text "else"; q.breakable; + q.group { + q.nest(2) { + q.group {q.text "a"; q.breakable; q.text "+"} + q.breakable; q.text "b"}}}}} } end @@ -780,17 +780,17 @@ End class TailGroup < Test::Unit::TestCase def test_1 - out = PrettyPrint.format('', 10) {|pp| - pp.group { - pp.group { - pp.text "abc" - pp.breakable - pp.text "def" + out = PrettyPrint.format('', 10) {|q| + q.group { + q.group { + q.text "abc" + q.breakable + q.text "def" } - pp.group { - pp.text "ghi" - pp.breakable - pp.text "jkl" + q.group { + q.text "ghi" + q.breakable + q.text "jkl" } } } @@ -800,10 +800,10 @@ End class NonString < Test::Unit::TestCase def format(width) - PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|pp| - pp.text(3, 3) - pp.breakable(1, 1) - pp.text(3, 3) + PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q| + q.text(3, 3) + q.breakable(1, 1) + q.text(3, 3) } end @@ -819,21 +819,21 @@ End class Fill < Test::Unit::TestCase def format(width) - PrettyPrint.format('', width) {|pp| - pp.group { - pp.text 'abc' - pp.fill_breakable - pp.text 'def' - pp.fill_breakable - pp.text 'ghi' - pp.fill_breakable - pp.text 'jkl' - pp.fill_breakable - pp.text 'mno' - pp.fill_breakable - pp.text 'pqr' - pp.fill_breakable - pp.text 'stu' + PrettyPrint.format('', width) {|q| + q.group { + q.text 'abc' + q.fill_breakable + q.text 'def' + q.fill_breakable + q.text 'ghi' + q.fill_breakable + q.text 'jkl' + q.fill_breakable + q.text 'mno' + q.fill_breakable + q.text 'pqr' + q.fill_breakable + q.text 'stu' } } end -- cgit v1.2.3