Module: PP::PPMethods
- Included in:
- PP, SingleLine
- Defined in:
- lib/pp-colour/temp.rb
Constant Summary collapse
- InspectKey =
:__inspect_key__
Instance Method Summary collapse
-
#comma_breakable ⇒ Object
A convenience method which is same as follows:.
- #guard_inspect_key ⇒ Object
- #object_address_group(obj, &block) ⇒ Object
-
#object_group(obj, &block) ⇒ Object
A convenience method which is same as follows:.
-
#pp(obj) ⇒ Object
Adds
obj
to the pretty printing buffer using Object#pretty_print or Object#pretty_print_cycle. - #pp_hash(obj) ⇒ Object
- #pp_object(obj) ⇒ Object
-
#seplist(list, sep = nil, iter_method = :each) ⇒ Object
Adds a separated list.
Instance Method Details
#comma_breakable ⇒ Object
A convenience method which is same as follows:
text ','
breakable
145 146 147 148 |
# File 'lib/pp-colour/temp.rb', line 145 def comma_breakable text ','.blue breakable end |
#guard_inspect_key ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/pp-colour/temp.rb', line 92 def guard_inspect_key if Thread.current[InspectKey] == nil Thread.current[InspectKey] = [] end save = Thread.current[InspectKey] begin Thread.current[InspectKey] = [] yield ensure Thread.current[InspectKey] = save end end |
#object_address_group(obj, &block) ⇒ Object
135 136 137 138 139 |
# File 'lib/pp-colour/temp.rb', line 135 def object_address_group(obj, &block) id = "%x" % (obj.__id__ * 2) id.sub!(/\Af(?=[[:xdigit:]]{2}+\z)/, '') if id.sub!(/\A\.\./, '') group(1, "\#<#{obj.class.to_s.magenta}:".green + "0x#{id}".blue, '>'.green, &block) end |
#object_group(obj, &block) ⇒ Object
A convenience method which is same as follows:
group(1, '#<' + obj.class.name, '>') { ... }
131 132 133 |
# File 'lib/pp-colour/temp.rb', line 131 def object_group(obj, &block) # :yield: group(1, '#<'.green + obj.class.name.magenta, '>'.green, &block) end |
#pp(obj) ⇒ Object
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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/pp-colour/temp.rb', line 112 def pp(obj) id = obj.__id__ 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 PP.sharing_detection end end |
#pp_hash(obj) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/pp-colour/temp.rb', line 202 def pp_hash(obj) group(1, '{'.green, '}'.green) { seplist(obj, nil, :each_pair) {|k, v| group { pp k text '=>'.blue group(1) { breakable '' pp v } } } } end |
#pp_object(obj) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/pp-colour/temp.rb', line 187 def pp_object(obj) object_address_group(obj) { seplist(obj.pretty_print_instance_variables, lambda { text ','.green }) {|v| breakable v = v.to_s if Symbol === v text v text '=' group(1) { breakable '' pp(obj.instance_eval(v)) } } } end |
#seplist(list, sep = nil, iter_method = :each) ⇒ Object
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
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/pp-colour/temp.rb', line 174 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 |