summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Lo <[email protected]>2023-03-13 22:31:30 +0800
committergit <[email protected]>2023-03-13 14:31:37 +0000
commit1095baed34dca15b9d8c6c54ea2f89bbaf67fb52 (patch)
treeb3fdbae6231cdacffa7053c021edf266531aef50
parent8c6b349805e2f17a57576b8dfad31e5681d6b0e9 (diff)
[ruby/irb] Support inspecting BasicObject
(https://github.com/ruby/irb/pull/541) https://github.com/ruby/irb/commit/1dc2a406a3
-rw-r--r--lib/irb/color_printer.rb8
-rw-r--r--test/irb/test_context.rb14
2 files changed, 20 insertions, 2 deletions
diff --git a/lib/irb/color_printer.rb b/lib/irb/color_printer.rb
index 1127bcecb4..a4eeb140cd 100644
--- a/lib/irb/color_printer.rb
+++ b/lib/irb/color_printer.rb
@@ -4,6 +4,10 @@ require_relative 'color'
module IRB
class ColorPrinter < ::PP
+ METHOD_IS_A = Object.instance_method(:is_a?)
+ METHOD_RESPOND_TO = Object.instance_method(:respond_to?)
+ METHOD_INSPECT = Object.instance_method(:inspect)
+
class << self
def pp(obj, out = $>, width = screen_width)
q = ColorPrinter.new(out, width)
@@ -22,9 +26,11 @@ module IRB
end
def pp(obj)
- if obj.is_a?(String)
+ if METHOD_IS_A.bind(obj).call(String)
# Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n"
text(obj.inspect)
+ elsif !METHOD_RESPOND_TO.bind(obj).call(:inspect)
+ text(METHOD_INSPECT.bind(obj).call)
else
super
end
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb
index 67d272601b..783b19b970 100644
--- a/test/irb/test_context.rb
+++ b/test/irb/test_context.rb
@@ -129,7 +129,6 @@ module TestIRB
failed: [
[false, "BasicObject.new", /#<NoMethodError: undefined method `to_s' for/],
[:p, "class Foo; undef inspect ;end; Foo.new", /#<NoMethodError: undefined method `inspect' for/],
- [true, "BasicObject.new", /#<NoMethodError: undefined method `is_a\?' for/],
[:yaml, "BasicObject.new", /#<NoMethodError: undefined method `inspect' for/],
[:marshal, "[Object.new, Class.new]", /#<TypeError: can't dump anonymous class #<Class:/]
]
@@ -150,6 +149,19 @@ module TestIRB
end
end
+ def test_object_inspection_handles_basic_object
+ verbose, $VERBOSE = $VERBOSE, nil
+ irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), TestInputMethod.new(["BasicObject.new"]))
+ out, err = capture_output do
+ irb.eval_input
+ end
+ assert_empty err
+ assert_not_match(/NoMethodError/, out)
+ assert_match(/#<BasicObject:.*>/, out)
+ ensure
+ $VERBOSE = verbose
+ end
+
def test_object_inspection_falls_back_to_kernel_inspect_when_errored
verbose, $VERBOSE = $VERBOSE, nil
main = Object.new