diff options
| author | Jason Frey <fryguy9@gmail.com> | 2025-08-13 13:37:34 -0400 |
|---|---|---|
| committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2025-10-05 15:13:50 +0900 |
| commit | 80a18e8f422e30204e7386fc9a1fc37667b20b2a (patch) | |
| tree | f8430f4c79f17e06df9d6d332942e47766db31dd | |
| parent | 8cc5e5c11d26b2af9acff9898c2b226e2e781e36 (diff) | |
[ruby/pp] Support new instance_variables_to_inspect method from Ruby core
This supports the new `instance_variables_to_inspect` method from Ruby
core that was added in ruby/ruby#13555.
If `instance_variables_to_inspect` is defined, then
`pretty_print_instance_variables` will use it.
Additionally, this commit introduces tests for both
`pretty_print_instance_variables` and `instance_variables_to_inspect`.
https://github.com/ruby/pp/commit/9cea466c95
| -rw-r--r-- | lib/pp.rb | 3 | ||||
| -rw-r--r-- | test/test_pp.rb | 14 |
2 files changed, 16 insertions, 1 deletions
@@ -399,7 +399,8 @@ class PP < PrettyPrint # This method should return an array of names of instance variables as symbols or strings as: # +[:@a, :@b]+. def pretty_print_instance_variables - instance_variables.sort + ivars = respond_to?(:instance_variables_to_inspect) ? instance_variables_to_inspect : instance_variables + ivars.sort end # Is #inspect implementation using #pretty_print. diff --git a/test/test_pp.rb b/test/test_pp.rb index c71445c9bc..e721260e01 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -130,6 +130,20 @@ class PPInspectTest < Test::Unit::TestCase assert_equal("#{a.inspect}\n", result) end + def test_iv_hiding + a = Object.new + def a.pretty_print_instance_variables() [:@b] end + a.instance_eval { @a = "aaa"; @b = "bbb" } + assert_match(/\A#<Object:0x[\da-f]+ @b="bbb">\n\z/, PP.pp(a, ''.dup)) + end + + def test_iv_hiding_via_ruby + a = Object.new + def a.instance_variables_to_inspect() [:@b] end + a.instance_eval { @a = "aaa"; @b = "bbb" } + assert_match(/\A#<Object:0x[\da-f]+ @b="bbb">\n\z/, PP.pp(a, ''.dup)) + end + def test_basic_object a = BasicObject.new assert_match(/\A#<BasicObject:0x[\da-f]+>\n\z/, PP.pp(a, ''.dup)) |
