diff options
author | Nobuyoshi Nakada <[email protected]> | 2021-09-05 21:51:14 +0900 |
---|---|---|
committer | git <[email protected]> | 2024-02-21 16:45:00 +0000 |
commit | 37b8fc7477b1cbeb4f3a21ad4b22971170709cfa (patch) | |
tree | e18ac7d14f21707953bcb66c293d9ce55686867d | |
parent | 82a4c3af1629298524024494fd065d51ff6ad5c6 (diff) |
[ruby/pp] Get rid of hardcoded class name
So that the `pp` method can work in inherited classes with that
class.
https://github.com/ruby/pp/commit/f204df3aad
-rw-r--r-- | lib/pp.rb | 2 | ||||
-rw-r--r-- | test/test_pp.rb | 32 |
2 files changed, 33 insertions, 1 deletions
@@ -93,7 +93,7 @@ class PP < PrettyPrint # # PP.pp returns +out+. def PP.pp(obj, out=$>, width=width_for(out)) - q = PP.new(out, width) + q = new(out, width) q.guard_inspect_key {q.pp obj} q.flush #$pp = q diff --git a/test/test_pp.rb b/test/test_pp.rb index f6bd2162d0..e51adde13e 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -245,4 +245,36 @@ if defined?(RubyVM) end end +class PPInheritedTest < Test::Unit::TestCase + class PPSymbolHash < PP + def pp_hash(obj) + group(1, "{", "}") { + seplist(obj, nil, :each_pair) {|k, v| + case k + when Symbol + text k.inspect.delete_prefix(":") + text ":" + sep = " " + else + pp k + text "=>" + sep = "" + end + group(1) { + breakable sep + pp v + } + } + } + end + end + + def test_hash_override + obj = {k: 1, "": :null, "0": :zero, 100 => :ten} + assert_equal <<~EXPECT, PPSymbolHash.pp(obj, "".dup) + {k: 1, "": :null, "0": :zero, 100=>:ten} + EXPECT + end +end + end |