[ruby/irb] Simplify the help command's implementation

(https://github.com/ruby/irb/pull/564)

The current method-redefining approach brings little benefit, makes it
harder to understand the code, and causes warnings like:

> warning: method redefined; discarding old execute

This patch simplifies it while displaying more helpful message when rdoc
couldn't be loaded.
This commit is contained in:
Stan Lo 2023-04-24 15:10:32 +01:00 committed by git
parent 805899dda2
commit 73fc81199d
2 changed files with 17 additions and 17 deletions

View File

@ -26,17 +26,15 @@ module IRB
def execute(*names) def execute(*names)
require 'rdoc/ri/driver' require 'rdoc/ri/driver'
opts = RDoc::RI::Driver.process_args([])
IRB::ExtendCommand::Help.const_set(:Ri, RDoc::RI::Driver.new(opts)) unless self.class.const_defined?(:Ri)
rescue LoadError, SystemExit opts = RDoc::RI::Driver.process_args([])
IRB::ExtendCommand::Help.remove_method(:execute) self.class.const_set(:Ri, RDoc::RI::Driver.new(opts))
# raise NoMethodError in ensure end
else
def execute(*names) if names.empty?
if names.empty? Ri.interactive
Ri.interactive else
return
end
names.each do |name| names.each do |name|
begin begin
Ri.display_name(name.to_s) Ri.display_name(name.to_s)
@ -44,11 +42,11 @@ module IRB
puts $!.message puts $!.message
end end
end end
nil
end end
nil nil
ensure rescue LoadError, SystemExit
execute(*names) warn "Can't display document because `rdoc` is not installed."
end end
end end
end end

View File

@ -785,7 +785,7 @@ module TestIRB
class ShowDocTest < CommandTestCase class ShowDocTest < CommandTestCase
def test_help_and_show_doc def test_help_and_show_doc
["help", "show_doc"].each do |cmd| ["help", "show_doc"].each do |cmd|
out, _ = execute_lines( out, err = execute_lines(
"#{cmd} String#gsub\n", "#{cmd} String#gsub\n",
"\n", "\n",
) )
@ -793,6 +793,7 @@ module TestIRB
# the former is what we'd get without document content installed, like on CI # the former is what we'd get without document content installed, like on CI
# the latter is what we may get locally # the latter is what we may get locally
possible_rdoc_output = [/Nothing known about String#gsub/, /gsub\(pattern\)/] possible_rdoc_output = [/Nothing known about String#gsub/, /gsub\(pattern\)/]
assert_empty err
assert(possible_rdoc_output.any? { |output| output.match?(out) }, "Expect the `#{cmd}` command to match one of the possible outputs. Got:\n#{out}") assert(possible_rdoc_output.any? { |output| output.match?(out) }, "Expect the `#{cmd}` command to match one of the possible outputs. Got:\n#{out}")
end end
ensure ensure
@ -801,7 +802,7 @@ module TestIRB
end end
def test_show_doc_without_rdoc def test_show_doc_without_rdoc
out, _ = without_rdoc do out, err = without_rdoc do
execute_lines( execute_lines(
"show_doc String#gsub\n", "show_doc String#gsub\n",
"\n", "\n",
@ -809,7 +810,8 @@ module TestIRB
end end
# if it fails to require rdoc, it only returns the command object # if it fails to require rdoc, it only returns the command object
assert_match(/=> IRB::ExtendCommand::Help\n/, out) assert_match(/=> nil\n/, out)
assert_include(err, "Can't display document because `rdoc` is not installed.\n")
ensure ensure
# this is the only way to reset the redefined method without coupling the test with its implementation # this is the only way to reset the redefined method without coupling the test with its implementation
EnvUtil.suppress_warning { load "irb/cmd/help.rb" } EnvUtil.suppress_warning { load "irb/cmd/help.rb" }