Module: PP::PPMethods
- Included in:
- PP
- Defined in:
- lib/pp.rb
Constant Summary
- PointerMask =
(1 << ([""].pack("p").size * 8)) - 1
Instance Method Summary (collapse)
- - (Object) check_inspect_key(id)
-
- (Object) comma_breakable
A convenience method which is same as follows:.
- - (Object) guard_inspect_key
- - (Object) object_address_group(obj, &block)
-
- (Object) object_group(obj, &block)
A convenience method which is same as follows:.
- - (Object) pop_inspect_key(id)
-
- (Object) pp(obj)
Adds obj to the pretty printing buffer using Object#pretty_print or Object#pretty_print_cycle.
- - (Object) pp_hash(obj)
- - (Object) pp_object(obj)
- - (Object) push_inspect_key(id)
-
- (Object) seplist(list, sep = nil, iter_method = :each)
Adds a separated list.
Instance Method Details
- (Object) check_inspect_key(id)
127 128 129 130 131 |
# File 'lib/pp.rb', line 127 def check_inspect_key(id) Thread.current[:__recursive_key__] && Thread.current[:__recursive_key__][:inspect] && Thread.current[:__recursive_key__][:inspect].include?(id) end |
- (Object) comma_breakable
A convenience method which is same as follows:
text ','
breakable
185 186 187 188 |
# File 'lib/pp.rb', line 185 def comma_breakable text ',' breakable end |
- (Object) guard_inspect_key
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/pp.rb', line 108 def guard_inspect_key if Thread.current[:__recursive_key__] == nil Thread.current[:__recursive_key__] = {}.untrust end if Thread.current[:__recursive_key__][:inspect] == nil Thread.current[:__recursive_key__][:inspect] = {}.untrust end save = Thread.current[:__recursive_key__][:inspect] begin Thread.current[:__recursive_key__][:inspect] = {}.untrust yield ensure Thread.current[:__recursive_key__][:inspect] = save end end |
- (Object) object_address_group(obj, &block)
176 177 178 179 |
# File 'lib/pp.rb', line 176 def object_address_group(obj, &block) id = PointerFormat % (obj.object_id * 2 & PointerMask) group(1, "\#<#{obj.class}:0x#{id}", '>', &block) end |
- (Object) object_group(obj, &block)
A convenience method which is same as follows:
group(1, '#<' + obj.class.name, '>') { ... }
163 164 165 |
# File 'lib/pp.rb', line 163 def object_group(obj, &block) # :yield: group(1, '#<' + obj.class.name, '>', &block) end |
- (Object) pop_inspect_key(id)
135 136 137 |
# File 'lib/pp.rb', line 135 def pop_inspect_key(id) Thread.current[:__recursive_key__][:inspect].delete id end |
- (Object) pp(obj)
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.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/pp.rb', line 144 def pp(obj) id = obj.object_id if check_inspect_key(id) group {obj.pretty_print_cycle self} return end begin push_inspect_key(id) group {obj.pretty_print self} ensure pop_inspect_key(id) unless PP.sharing_detection end end |
- (Object) pp_hash(obj)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/pp.rb', line 242 def pp_hash(obj) group(1, '{', '}') { seplist(obj, nil, :each_pair) {|k, v| group { pp k text '=>' group(1) { breakable '' pp v } } } } end |
- (Object) pp_object(obj)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/pp.rb', line 227 def pp_object(obj) object_address_group(obj) { seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v| breakable v = v.to_s if Symbol === v text v text '=' group(1) { breakable '' pp(obj.instance_eval(v)) } } } end |
- (Object) push_inspect_key(id)
132 133 134 |
# File 'lib/pp.rb', line 132 def push_inspect_key(id) Thread.current[:__recursive_key__][:inspect][id] = true end |
- (Object) seplist(list, sep = nil, iter_method = :each)
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 { q.comma_breakable }, :each) {|v| xxx v }
xxx 1
q.comma_breakable
xxx 2
q.comma_breakable
xxx 3
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/pp.rb', line 214 def seplist(list, sep=nil, iter_method=:each) # :yield: element sep ||= lambda { comma_breakable } first = true list.__send__(iter_method) {|*v| if first first = false else sep.call end yield(*v) } end |