diff options
author | Stan Lo <stan001212@gmail.com> | 2023-02-27 19:07:14 +0800 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-02-27 11:07:19 +0000 |
commit | 0aa50a03b1ea8d37069ae57c469f43860abbcf05 (patch) | |
tree | e489108876b1dd857eb888f9679f89196c443dcb /lib | |
parent | 4f611df3f7f61fbdf83b02121dd1edea0b7c68ec (diff) | |
download | ruby-0aa50a03b1ea8d37069ae57c469f43860abbcf05.tar.gz |
[ruby/irb] Provide more useful message when
`IRB::Inspector#inspect_value` errors
(https://github.com/ruby/irb/pull/511)
**Before**
```
irb(main):001:0> c = Cat.new "foo"
(Object doesn't support #inspect)
=>
```
**After**
```
irb(main):001:0> c = Cat.new "foo"
An error occurred when inspecting the object: #<NoMethodError: undefined method `is_a?' for foo:Cat
if obj.is_a?(String)
^^^^^^>
Result of Kernel#inspect: #<Cat:0x0000000109090d80 @name="foo">
=>
```
Diffstat (limited to 'lib')
-rw-r--r-- | lib/irb/inspector.rb | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/irb/inspector.rb b/lib/irb/inspector.rb index 50f6ebf383..bdfa282f0d 100644 --- a/lib/irb/inspector.rb +++ b/lib/irb/inspector.rb @@ -35,6 +35,7 @@ module IRB # :nodoc: # irb(main):001:0> "what?" #=> omg! what? # class Inspector + KERNEL_INSPECT = Object.instance_method(:inspect) # Default inspectors available to irb, this includes: # # +:pp+:: Using Kernel#pretty_inspect @@ -93,9 +94,18 @@ module IRB # :nodoc: # Proc to call when the input is evaluated and output in irb. def inspect_value(v) @inspect.call(v) - rescue - puts "(Object doesn't support #inspect)" - '' + rescue => e + puts "An error occurred when inspecting the object: #{e.inspect}" + + begin + # TODO: change this to bind_call when we drop support for Ruby 2.6 + puts "Result of Kernel#inspect: #{KERNEL_INSPECT.bind(v).call}" + '' + rescue => e + puts "An error occurred when running Kernel#inspect: #{e.inspect}" + puts e.backtrace.join("\n") + '' + end end end |