Module | PP::PPMethods |
In: |
lib/pp.rb
|
InspectKey | = | :__inspect_key__ |
A convenience method which is same as follows:
text ',' breakable
# File lib/pp.rb, line 163 163: def comma_breakable 164: text ',' 165: breakable 166: end
# File lib/pp.rb, line 110 110: def guard_inspect_key 111: if Thread.current[InspectKey] == nil 112: Thread.current[InspectKey] = [] 113: end 114: 115: save = Thread.current[InspectKey] 116: 117: begin 118: Thread.current[InspectKey] = [] 119: yield 120: ensure 121: Thread.current[InspectKey] = save 122: end 123: end
# File lib/pp.rb, line 153 153: def object_address_group(obj, &block) 154: id = "%x" % (obj.__id__ * 2) 155: id.sub!(/\Af(?=[[:xdigit:]]{2}+\z)/, '') if id.sub!(/\A\.\./, '') 156: group(1, "\#<#{obj.class}:0x#{id}", '>', &block) 157: end
A convenience method which is same as follows:
group(1, '#<' + obj.class.name, '>') { ... }
# File lib/pp.rb, line 149 149: def object_group(obj, &block) # :yield: 150: group(1, '#<' + obj.class.name, '>', &block) 151: end
Adds obj to the pretty printing buffer using Object#pretty_print or Object#pretty_print_cycle.
Object#pretty_print_cycle is used when obj is already printed, a.k.a the object reference chain has a cycle.
# File lib/pp.rb, line 130 130: def pp(obj) 131: id = obj.__id__ 132: 133: if Thread.current[InspectKey].include? id 134: group {obj.pretty_print_cycle self} 135: return 136: end 137: 138: begin 139: Thread.current[InspectKey] << id 140: group {obj.pretty_print self} 141: ensure 142: Thread.current[InspectKey].pop unless PP.sharing_detection 143: end 144: end
# File lib/pp.rb, line 220 220: def pp_hash(obj) 221: group(1, '{', '}') { 222: seplist(obj, nil, :each_pair) {|k, v| 223: group { 224: pp k 225: text '=>' 226: group(1) { 227: breakable '' 228: pp v 229: } 230: } 231: } 232: } 233: end
# File lib/pp.rb, line 205 205: def pp_object(obj) 206: object_address_group(obj) { 207: seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v| 208: breakable 209: v = v.to_s if Symbol === v 210: text v 211: text '=' 212: group(1) { 213: breakable '' 214: pp(obj.instance_eval(v)) 215: } 216: } 217: } 218: end
Adds a separated list. The list is separated by comma with breakable space, by default.
seplist iterates the list using iter_method. It yields each object to the block given for seplist. The procedure separator_proc is called between each yields.
If the iteration is zero times, separator_proc is not called at all.
If separator_proc is nil or not given, +lambda { comma_breakable }+ is used. If iter_method is not given, :each is used.
For example, following 3 code fragments has similar effect.
q.seplist([1,2,3]) {|v| xxx v } q.seplist([1,2,3], lambda { comma_breakable }, :each) {|v| xxx v } xxx 1 q.comma_breakable xxx 2 q.comma_breakable xxx 3
# File lib/pp.rb, line 192 192: def seplist(list, sep=nil, iter_method=:each) # :yield: element 193: sep ||= lambda { comma_breakable } 194: first = true 195: list.__send__(iter_method) {|*v| 196: if first 197: first = false 198: else 199: sep.call 200: end 201: yield(*v) 202: } 203: end