Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ def pretty_print_cycle(q)
# 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.
Expand Down
14 changes: 14 additions & 0 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ def a.to_s() "aaa" end
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))
Expand Down