1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/pp.rb (PP.singleline_pp): new method.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3110 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2002-12-02 16:16:05 +00:00
parent 232fda5237
commit 96f82b243f
2 changed files with 85 additions and 69 deletions

View file

@ -1,3 +1,7 @@
Tue Dec 3 01:13:41 2002 Tanaka Akira <akr@m17n.org>
* lib/pp.rb (PP.singleline_pp): new method.
Sun Dec 1 23:04:03 2002 Nobuyoshi Nakada <nobu.nokada@softhome.net> Sun Dec 1 23:04:03 2002 Nobuyoshi Nakada <nobu.nokada@softhome.net>
* lib/optparse.rb (OptionParser::new): same as OptionParser#on but * lib/optparse.rb (OptionParser::new): same as OptionParser#on but

150
lib/pp.rb
View file

@ -78,6 +78,12 @@ PP#pp to print the object.
PP.pp returns ((|out|)). PP.pp returns ((|out|)).
--- PP.singleline_pp(obj[, out])
outputs ((|obj|)) to ((|out|)) like (({PP.pp})) but with no indent and
newline.
PP.singleline_pp returns ((|out|)).
--- PP.sharing_detection --- PP.sharing_detection
returns the sharing detection flag as a boolean value. returns the sharing detection flag as a boolean value.
It is false by default. It is false by default.
@ -148,96 +154,102 @@ class PP < PrettyPrint
out << "\n" out << "\n"
end end
@@sharing_detection = false def PP.singleline_pp(obj, out=$>)
def PP.sharing_detection pp = SingleLine.new(out)
return @@sharing_detection pp.guard_inspect_key {pp.pp obj}
pp.flush
out
end end
def PP.sharing_detection=(val) @sharing_detection = false
@@sharing_detection = val class << self
attr_accessor :sharing_detection
end end
def initialize(out, width=79) module PPMethods
super InspectKey = :__inspect_key__
@sharing_detection = @@sharing_detection
end
InspectKey = :__inspect_key__ def guard_inspect_key
if Thread.current[InspectKey] == nil
Thread.current[InspectKey] = []
end
def guard_inspect_key save = Thread.current[InspectKey]
if Thread.current[InspectKey] == nil
Thread.current[InspectKey] = [] begin
Thread.current[InspectKey] = []
yield
ensure
Thread.current[InspectKey] = save
end
end end
save = Thread.current[InspectKey] def pp(obj)
id = obj.__id__
begin if Thread.current[InspectKey].include? id
Thread.current[InspectKey] = [] group {obj.pretty_print_cycle self}
yield return
ensure end
Thread.current[InspectKey] = save
end
end
def pp(obj) begin
id = obj.__id__ Thread.current[InspectKey] << id
group {obj.pretty_print self}
if Thread.current[InspectKey].include? id ensure
group {obj.pretty_print_cycle self} Thread.current[InspectKey].pop unless PP.sharing_detection
return end
end end
begin def object_group(obj, &block)
Thread.current[InspectKey] << id group(1, '#<' + obj.class.name, '>', &block)
group {obj.pretty_print self}
ensure
Thread.current[InspectKey].pop unless @sharing_detection
end end
end
def object_group(obj, &block) def object_address_group(obj, &block)
group(1, '#<' + obj.class.name, '>', &block) group(1, sprintf('#<%s:0x%x', obj.class.name, obj.__id__ * 2), '>', &block)
end end
def object_address_group(obj, &block) def comma_breakable
group(1, sprintf('#<%s:0x%x', obj.class.name, obj.__id__ * 2), '>', &block) text ','
end breakable
end
def comma_breakable def pp_object(obj)
text ',' object_address_group(obj) {
breakable obj.pretty_print_instance_variables.each {|v|
end v = v.to_s if Symbol === v
text ',' unless first?
def pp_object(obj) breakable
object_address_group(obj) { text v
obj.pretty_print_instance_variables.each {|v| text '='
v = v.to_s if Symbol === v
text ',' unless first?
breakable
text v
text '='
group(1) {
breakable ''
pp(obj.instance_eval(v))
}
}
}
end
def pp_hash(obj)
group(1, '{', '}') {
obj.each {|k, v|
comma_breakable unless first?
group {
pp k
text '=>'
group(1) { group(1) {
breakable '' breakable ''
pp v pp(obj.instance_eval(v))
} }
} }
} }
} end
def pp_hash(obj)
group(1, '{', '}') {
obj.each {|k, v|
comma_breakable unless first?
group {
pp k
text '=>'
group(1) {
breakable ''
pp v
}
}
}
}
end
end
include PPMethods
class SingleLine < PrettyPrint::SingleLine
include PPMethods
end end
module ObjectMixin module ObjectMixin