Skip to content

Commit

Permalink
[ruby/irb] Simplify the help command's implementation
Browse files Browse the repository at this point in the history
(ruby/irb#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.
  • Loading branch information
st0012 authored and matzbot committed Apr 24, 2023
1 parent 805899d commit 73fc811
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 12 additions & 14 deletions lib/irb/cmd/help.rb
Expand Up @@ -26,29 +26,27 @@ def transform_args(args)

def execute(*names)
require 'rdoc/ri/driver'
opts = RDoc::RI::Driver.process_args([])
IRB::ExtendCommand::Help.const_set(:Ri, RDoc::RI::Driver.new(opts))
rescue LoadError, SystemExit
IRB::ExtendCommand::Help.remove_method(:execute)
# raise NoMethodError in ensure
else
def execute(*names)
if names.empty?
Ri.interactive
return
end

unless self.class.const_defined?(:Ri)
opts = RDoc::RI::Driver.process_args([])
self.class.const_set(:Ri, RDoc::RI::Driver.new(opts))
end

if names.empty?
Ri.interactive
else
names.each do |name|
begin
Ri.display_name(name.to_s)
rescue RDoc::RI::Error
puts $!.message
end
end
nil
end

nil
ensure
execute(*names)
rescue LoadError, SystemExit
warn "Can't display document because `rdoc` is not installed."
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions test/irb/test_cmd.rb
Expand Up @@ -785,14 +785,15 @@ def test_ls_with_no_singleton_class
class ShowDocTest < CommandTestCase
def test_help_and_show_doc
["help", "show_doc"].each do |cmd|
out, _ = execute_lines(
out, err = execute_lines(
"#{cmd} String#gsub\n",
"\n",
)

# the former is what we'd get without document content installed, like on CI
# the latter is what we may get locally
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}")
end
ensure
Expand All @@ -801,15 +802,16 @@ def test_help_and_show_doc
end

def test_show_doc_without_rdoc
out, _ = without_rdoc do
out, err = without_rdoc do
execute_lines(
"show_doc String#gsub\n",
"\n",
)
end

# 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
# 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" }
Expand Down

0 comments on commit 73fc811

Please sign in to comment.