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
31 changes: 10 additions & 21 deletions lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,22 @@ module PPMethods
# Yields to a block
# and preserves the previous set of objects being printed.
def guard_inspect_key
if Thread.current[:__recursive_key__] == nil
Thread.current[:__recursive_key__] = {}.compare_by_identity
end

if Thread.current[:__recursive_key__][:inspect] == nil
Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
end

save = Thread.current[:__recursive_key__][:inspect]

recursive_state = Thread.current[:__recursive_key__] ||= {}.compare_by_identity
save = recursive_state[:inspect] ||= {}.compare_by_identity
begin
Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
recursive_state[:inspect] = {}.compare_by_identity
yield
ensure
Thread.current[:__recursive_key__][:inspect] = save
recursive_state[:inspect] = save
end
end

# 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.
def check_inspect_key(id)
Thread.current[:__recursive_key__] &&
Thread.current[:__recursive_key__][:inspect] &&
Thread.current[:__recursive_key__][:inspect].include?(id)
recursive_state = Thread.current[:__recursive_key__] or return false
recursive_state[:inspect]&.include?(id)
end

# Adds the object_id +id+ to the set of objects being pretty printed, so
Expand All @@ -186,7 +177,7 @@ def pop_inspect_key(id)
private def guard_inspect(object)
recursive_state = Thread.current[:__recursive_key__]

if recursive_state && recursive_state.key?(:inspect)
if recursive_state&.key?(:inspect)
begin
push_inspect_key(object)
yield
Expand Down Expand Up @@ -322,12 +313,10 @@ def pp_hash(obj)
# A pretty print for a pair of Hash
def pp_hash_pair(k, v)
if Symbol === k
sym_s = k.inspect
if sym_s[1].match?(/["$@!]/) || sym_s[-1].match?(/[%&*+\-\/<=>@\]^`|~]/)
text "#{k.to_s.inspect}:"
else
text "#{k}:"
if k.inspect.match?(%r[\A:["$@!]|[%&*+\-\/<=>@\]^`|~]\z])
k = k.to_s.inspect
end
text "#{k}:"
else
pp k
text ' '
Expand Down
20 changes: 16 additions & 4 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ def test_hash
end

def test_hash_symbol_colon_key
omit if RUBY_VERSION < "3.4."
no_quote = "{a: 1, a!: 1, a?: 1}"
unicode_quote = "{\u{3042}: 1}"
quote0 = '{"": 1}'
Expand All @@ -270,12 +269,25 @@ def test_hash_symbol_colon_key
assert_equal(quote1, PP.singleline_pp(eval(quote1), ''.dup))
assert_equal(quote2, PP.singleline_pp(eval(quote2), ''.dup))
assert_equal(quote3, PP.singleline_pp(eval(quote3), ''.dup))
end
end if RUBY_VERSION >= "3.4."

def test_hash_in_array
omit if RUBY_ENGINE == "jruby"
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))
assert_equal("[{}]", passing_keywords {PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup)})
assert_equal("[{}]", passing_keywords {PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup)})
end

if RUBY_VERSION >= "3.0"
def passing_keywords(&_)
yield
end
else
def passing_keywords(&_)
verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = verbose
end
end

def test_direct_pp
Expand Down