Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ def pp(obj)
guard_inspect(obj) do
group do
obj.pretty_print self
rescue NoMethodError
rescue NoMethodError => error
raise unless
error.name == :pretty_print &&
error.receiver.equal?(obj) &&
!PP.mcall(obj, Kernel, :respond_to?, :pretty_print)
text Kernel.instance_method(:inspect).bind_call(obj)
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class PrettyPrintInspectWithoutPrettyPrint
alias inspect pretty_print_inspect
end

class PrettyPrintRaises
def pretty_print(q)
self.missing_from_pretty_print
end
end

class PPInspectTest < Test::Unit::TestCase
def test_hasinspect
a = HasInspect.new(1)
Expand Down Expand Up @@ -157,6 +163,11 @@ def test_basic_object
a = BasicObject.new
assert_match(/\A#<BasicObject:0x[\da-f]+>\n\z/, PP.pp(a, ''.dup))
end

def test_pretty_print_error
error = assert_raise(NoMethodError) { PP.pp(PrettyPrintRaises.new, ''.dup) }
assert_equal(:missing_from_pretty_print, error.name)
end
end

class PPCycleTest < Test::Unit::TestCase
Expand Down