Module: PP::PPMethods
- Included in:
- PP, SingleLine
- Defined in:
- lib/extensions/mspec/mspec/pp.rb
Constant Summary collapse
- PointerMask =
64bit
0xffffffffffffffff
Instance Method Summary collapse
- #check_inspect_key(id) ⇒ Object
-
#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:.
- #pop_inspect_key(id) ⇒ Object
-
#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
- #push_inspect_key(id) ⇒ Object
-
#seplist(list, sep = nil, iter_method = :each) ⇒ Object
Adds a separated list.
Instance Method Details
#check_inspect_key(id) ⇒ Object
503 504 505 506 507 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 503 def check_inspect_key(id) Thread.current[:__recursive_key__] && Thread.current[:__recursive_key__][:inspect] && Thread.current[:__recursive_key__][:inspect].include?(id) end |
#comma_breakable ⇒ Object
A convenience method which is same as follows:
text ','
breakable
567 568 569 570 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 567 def comma_breakable text ',' breakable end |
#guard_inspect_key ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 484 def guard_inspect_key if Thread.current[:__recursive_key__] == nil Thread.current[:__recursive_key__] = {} end if Thread.current[:__recursive_key__][:inspect] == nil Thread.current[:__recursive_key__][:inspect] = {} end save = Thread.current[:__recursive_key__][:inspect] begin Thread.current[:__recursive_key__][:inspect] = {} yield ensure Thread.current[:__recursive_key__][:inspect] = save end end |
#object_address_group(obj, &block) ⇒ Object
558 559 560 561 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 558 def object_address_group(obj, &block) id = PointerFormat % (obj.object_id * 2 & PointerMask) group(1, "\#<#{obj.class}:0x#{id}", '>', &block) end |
#object_group(obj, &block) ⇒ Object
A convenience method which is same as follows:
group(1, '#<' + obj.class.name, '>') { ... }
539 540 541 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 539 def object_group(obj, &block) # :yield: group(1, '#<' + obj.class.name, '>', &block) end |
#pop_inspect_key(id) ⇒ Object
511 512 513 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 511 def pop_inspect_key(id) Thread.current[:__recursive_key__][:inspect].delete id 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.
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 520 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) ⇒ Object
624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 624 def pp_hash(obj) group(1, '{', '}') { seplist(obj, nil, :each_pair) {|k, v| group { pp k text '=>' group(1) { breakable '' pp v } } } } end |
#pp_object(obj) ⇒ Object
609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 609 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) ⇒ Object
508 509 510 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 508 def push_inspect_key(id) Thread.current[:__recursive_key__][:inspect][id] = true 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 { q.comma_breakable }, :each) {|v| xxx v }
xxx 1
q.comma_breakable
xxx 2
q.comma_breakable
xxx 3
596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/extensions/mspec/mspec/pp.rb', line 596 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 |