Module: PP::PPMethods
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
::PP,
PP::SingleLine
| |
Defined in: | lib/pp.rb |
Instance Method Summary
-
#check_inspect_key(id)
Check whether the object_id
id
is in the current buffer of objects to be pretty printed. -
#comma_breakable
A convenience method which is same as follows:
-
#guard_inspect_key
Yields to a block and preserves the previous set of objects being printed.
-
#object_address_group(obj, &block)
A convenience method, like object_group, but also reformats the Object's object_id.
-
#object_group(obj, &block)
A convenience method which is same as follows:
-
#pop_inspect_key(id)
Removes an object from the set of objects being pretty printed.
-
#pp(obj)
Adds
obj
to the pretty printing buffer usingObject#pretty_print
orObject#pretty_print_cycle
. -
#pp_hash(obj)
A pretty print for a ::Hash.
-
#pp_object(obj)
A present standard failsafe for pretty printing any given ::Object.
-
#push_inspect_key(id)
Adds the object_id
id
to the set of objects being pretty printed, so as to not repeat objects. -
#seplist(list, sep = nil, iter_method = :each)
Adds a separated list.
Instance Method Details
#check_inspect_key(id)
Check whether the object_id id
is in the current buffer of objects to be pretty printed. Used to break cycles in chains of objects to be pretty printed.
# File 'lib/pp.rb', line 156
def check_inspect_key(id) Thread.current[:__recursive_key__] && Thread.current[:__recursive_key__][:inspect] && Thread.current[:__recursive_key__][:inspect].include?(id) end
#comma_breakable
A convenience method which is same as follows:
text ','
breakable
# File 'lib/pp.rb', line 213
def comma_breakable text ',' breakable end
#guard_inspect_key
Yields to a block and preserves the previous set of objects being printed.
# File 'lib/pp.rb', line 134
def guard_inspect_key if Thread.current[:__recursive_key__] == nil Thread.current[:__recursive_key__] = {}.taint end if Thread.current[:__recursive_key__][:inspect] == nil Thread.current[:__recursive_key__][:inspect] = {}.taint end save = Thread.current[:__recursive_key__][:inspect] begin Thread.current[:__recursive_key__][:inspect] = {}.taint yield ensure Thread.current[:__recursive_key__][:inspect] = save end end
#object_address_group(obj, &block)
A convenience method, like object_group, but also reformats the Object's object_id.
# File 'lib/pp.rb', line 203
def object_address_group(obj, &block) str = Kernel.instance_method(:to_s).bind(obj).call str.chomp!('>') group(1, str, '>', &block) end
#object_group(obj, &block)
A convenience method which is same as follows:
group(1, '#<' + obj.class.name, '>') { ... }
# File 'lib/pp.rb', line 197
def object_group(obj, &block) # :yield: group(1, '#<' + obj.class.name, '>', &block) end
#pop_inspect_key(id)
Removes an object from the set of objects being pretty printed.
# File 'lib/pp.rb', line 169
def pop_inspect_key(id) Thread.current[:__recursive_key__][:inspect].delete id end
#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.
# File 'lib/pp.rb', line 178
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
#pp_hash(obj)
A pretty print for a ::Hash
#pp_object(obj)
A present standard failsafe for pretty printing any given ::Object
# File 'lib/pp.rb', line 256
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
#push_inspect_key(id)
Adds the object_id id
to the set of objects being pretty printed, so as to not repeat objects.
# File 'lib/pp.rb', line 164
def push_inspect_key(id) Thread.current[:__recursive_key__][:inspect][id] = true end
#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
# File 'lib/pp.rb', line 242
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