summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-12-02 16:16:05 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-12-02 16:16:05 +0000
commit96f82b243fce324b33e71357564a15b51f6aa949 (patch)
treedfc419af9aaa4751e69d21acc7a152ff6972af35
parent232fda523767d5f91eff4ab8c38d84f30e356e6d (diff)
* 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
-rw-r--r--ChangeLog4
-rw-r--r--lib/pp.rb148
2 files changed, 84 insertions, 68 deletions
diff --git a/ChangeLog b/ChangeLog
index 2db3084..ed6c397 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
* lib/optparse.rb (OptionParser::new): same as OptionParser#on but
diff --git a/lib/pp.rb b/lib/pp.rb
index 489e342..3718840 100644
--- a/lib/pp.rb
+++ b/lib/pp.rb
@@ -78,6 +78,12 @@ PP#pp to print the object.
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
returns the sharing detection flag as a boolean value.
It is false by default.
@@ -148,96 +154,102 @@ class PP < PrettyPrint
out << "\n"
end
- @@sharing_detection = false
- def PP.sharing_detection
- return @@sharing_detection
- end
-
- def PP.sharing_detection=(val)
- @@sharing_detection = val
+ def PP.singleline_pp(obj, out=$>)
+ pp = SingleLine.new(out)
+ pp.guard_inspect_key {pp.pp obj}
+ pp.flush
+ out
end
- def initialize(out, width=79)
- super
- @sharing_detection = @@sharing_detection
+ @sharing_detection = false
+ class << self
+ attr_accessor :sharing_detection
end
- InspectKey = :__inspect_key__
+ module PPMethods
+ InspectKey = :__inspect_key__
- def guard_inspect_key
- if Thread.current[InspectKey] == nil
- Thread.current[InspectKey] = []
- end
+ def guard_inspect_key
+ if Thread.current[InspectKey] == nil
+ Thread.current[InspectKey] = []
+ end
- save = Thread.current[InspectKey]
+ save = Thread.current[InspectKey]
- begin
- Thread.current[InspectKey] = []
- yield
- ensure
- Thread.current[InspectKey] = save
+ begin
+ Thread.current[InspectKey] = []
+ yield
+ ensure
+ Thread.current[InspectKey] = save
+ end
end
- end
- def pp(obj)
- id = obj.__id__
+ def pp(obj)
+ id = obj.__id__
- if Thread.current[InspectKey].include? id
- group {obj.pretty_print_cycle self}
- return
- end
+ if Thread.current[InspectKey].include? id
+ group {obj.pretty_print_cycle self}
+ return
+ end
- begin
- Thread.current[InspectKey] << id
- group {obj.pretty_print self}
- ensure
- Thread.current[InspectKey].pop unless @sharing_detection
+ begin
+ Thread.current[InspectKey] << id
+ group {obj.pretty_print self}
+ ensure
+ Thread.current[InspectKey].pop unless PP.sharing_detection
+ end
end
- end
- def object_group(obj, &block)
- group(1, '#<' + obj.class.name, '>', &block)
- end
+ def object_group(obj, &block)
+ group(1, '#<' + obj.class.name, '>', &block)
+ end
- def object_address_group(obj, &block)
- group(1, sprintf('#<%s:0x%x', obj.class.name, obj.__id__ * 2), '>', &block)
- end
+ def object_address_group(obj, &block)
+ group(1, sprintf('#<%s:0x%x', obj.class.name, obj.__id__ * 2), '>', &block)
+ end
- def comma_breakable
- text ','
- breakable
- end
+ def comma_breakable
+ text ','
+ breakable
+ end
- def pp_object(obj)
- object_address_group(obj) {
- obj.pretty_print_instance_variables.each {|v|
- v = v.to_s if Symbol === v
- text ',' unless first?
- breakable
- text v
- text '='
- group(1) {
- breakable ''
- pp(obj.instance_eval(v))
+ def pp_object(obj)
+ object_address_group(obj) {
+ obj.pretty_print_instance_variables.each {|v|
+ v = v.to_s if Symbol === v
+ text ',' unless first?
+ breakable
+ text v
+ text '='
+ group(1) {
+ breakable ''
+ pp(obj.instance_eval(v))
+ }
}
}
- }
- end
+ end
- def pp_hash(obj)
- group(1, '{', '}') {
- obj.each {|k, v|
- comma_breakable unless first?
- group {
- pp k
- text '=>'
- group(1) {
- breakable ''
- pp v
+ 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
module ObjectMixin