Skip to content

Commit 8450e76

Browse files
authored
Support private instance_variables_to_inspect (#70)
* Support private instance_variables_to_inspect in pp Ruby supports calling instance_variables_to_inspect even when it is defined as a private method (ruby/ruby#13555). This change aligns pp with Ruby's behavior.
1 parent a13daec commit 8450e76

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

lib/pp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def pretty_print_cycle(q)
388388
# This method should return an array of names of instance variables as symbols or strings as:
389389
# +[:@a, :@b]+.
390390
def pretty_print_instance_variables
391-
ivars = respond_to?(:instance_variables_to_inspect) ? instance_variables_to_inspect : instance_variables
391+
ivars = respond_to?(:instance_variables_to_inspect, true) ? instance_variables_to_inspect || instance_variables : instance_variables
392392
ivars.sort
393393
end
394394

test/test_pp.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def a.pretty_print_instance_variables() [:@b] end
146146

147147
def test_iv_hiding_via_ruby
148148
a = Object.new
149-
def a.instance_variables_to_inspect() [:@b] end
149+
a.singleton_class.class_eval do
150+
private def instance_variables_to_inspect() [:@b] end
151+
end
150152
a.instance_eval { @a = "aaa"; @b = "bbb" }
151153
assert_match(/\A#<Object:0x[\da-f]+ @b="bbb">\n\z/, PP.pp(a, ''.dup))
152154
end

0 commit comments

Comments
 (0)