Skip to content

Conversation

@hamajyotan
Copy link
Contributor

This is related to #49.

In ruby/ruby#13555, instance_variables_to_inspect works even when it is defined as a private method.
In fact, the example shown in NEWS defines it as a private method.
https://github.com/ruby/ruby/blob/v4.0.0/NEWS.md?plain=1#L139-L155

I think it would be better for pp to follow this behavior as well.
Below is a minimal reproducible example demonstrating the difference.

require "pp"

class Foo
  def initialize
    @a = "aaa"
    @b = "bbb"
  end

  def instance_variables_to_inspect = [:@b]
end

a = Foo.new
b = Foo.new
b.singleton_class.class_eval { private :instance_variables_to_inspect }

puts RUBY_VERSION
puts a.inspect
puts PP.pp(a, ''.dup)
puts b.inspect
puts PP.pp(b, ''.dup)
#<Foo:0x00007f407f6964c0 @b="bbb">
#<Foo:0x00007f407f6964c0 @b="bbb">
#<Foo:0x00007f407f6961f0 @b="bbb">
#<Foo:0x00007f407f6961f0 @a="aaa", @b="bbb">

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.
@mame mame merged commit 8450e76 into ruby:master Dec 26, 2025
24 checks passed
matzbot pushed a commit to ruby/ruby that referenced this pull request Dec 26, 2025
(ruby/pp#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 (#13555).

This change aligns pp with Ruby's behavior.

ruby/pp@8450e76db6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants