String Formating for hexadecimal id
A mask used in formating object_id’s into a hexadecimal 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 pp.rb, line 155
def check_inspect_key(id)
Thread.current[:__recursive_key__] &&
Thread.current[:__recursive_key__][:inspect] &&
Thread.current[:__recursive_key__][:inspect].include?(id)
end
A convenience method which is same as follows:
text ',' breakable
# File pp.rb, line 222
def comma_breakable
text ','
breakable
end
Yields to a block and preserves the previous set of objects being printed.
# File pp.rb, line 133
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
A convenience method, like #object_group, but also reformats the Object’s object_id.
# File pp.rb, line 213
def object_address_group(obj, &block)
id = PointerFormat % (obj.object_id * 2 & PointerMask)
group(1, "\#<#{obj.class}:0x#{id}", '>', &block)
end
A convenience method which is same as follows:
group(1, '#<' + obj.class.name, '>') { ... }
# File pp.rb, line 196
def object_group(obj, &block) # :yield:
group(1, '#<' + obj.class.name, '>', &block)
end
Removes an object from the set of objects being pretty printed.
# File pp.rb, line 168
def pop_inspect_key(id)
Thread.current[:__recursive_key__][:inspect].delete id
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 pp.rb, line 177
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
A pretty print for a Hash
# File pp.rb, line 281
def pp_hash(obj)
group(1, '{', '}') {
seplist(obj, nil, :each_pair) {|k, v|
group {
pp k
text '=>'
group(1) {
breakable ''
pp v
}
}
}
}
end
A present standard failsafe for pretty printing any given Object
# File pp.rb, line 265
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
Adds the object_id id to the set of objects being pretty
printed, so as to not repeat objects.
# File pp.rb, line 163
def push_inspect_key(id)
Thread.current[:__recursive_key__][:inspect][id] = true
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 { q.comma_breakable }, :each) {|v| xxx v } xxx 1 q.comma_breakable xxx 2 q.comma_breakable xxx 3
# File pp.rb, line 251
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
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please see Improve the docs, or visit Documenting-ruby.org.