Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ def pop_inspect_key(id)
Thread.current[:__recursive_key__][:inspect].delete id
end

private def guard_inspect(object)
recursive_state = Thread.current[:__recursive_key__]

if recursive_state && recursive_state.key?(:inspect)
begin
push_inspect_key(object)
yield
ensure
pop_inspect_key(object) unless PP.sharing_detection
end
else
guard_inspect_key do
push_inspect_key(object)
yield
end
end
end

# Adds +obj+ to the pretty printing buffer
# using Object#pretty_print or Object#pretty_print_cycle.
#
Expand All @@ -198,15 +216,12 @@ def pp(obj)
return
end

begin
push_inspect_key(obj)
guard_inspect(obj) do
group do
obj.pretty_print self
rescue NoMethodError
text Kernel.instance_method(:inspect).bind_call(obj)
end
ensure
pop_inspect_key(obj) unless PP.sharing_detection
end
end

Expand Down
16 changes: 16 additions & 0 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@ def test_hash_in_array
assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
end

def test_direct_pp
buffer = String.new

a = []
a << a

# Isolate the test from any existing Thread.current[:__recursive_key__][:inspect].
Thread.new do
q = PP::SingleLine.new(buffer)
q.pp(a)
q.flush
end.join

assert_equal("[[...]]", buffer)
end
end

class PPDelegateTest < Test::Unit::TestCase
Expand Down