diff --git a/lib/pp.rb b/lib/pp.rb index 6060282..e790f49 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -145,21 +145,13 @@ 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 @@ -167,9 +159,8 @@ def guard_inspect_key # 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 @@ -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 @@ -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 ' ' diff --git a/test/test_pp.rb b/test/test_pp.rb index c5340a3..4a273e6 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -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}' @@ -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