Guard RDoc parser definition to avoid method redefined warning
When two copies of rbs are on the load path (e.g. an installed bundled
gem and a source checkout, as in `ruby/ruby`'s `test-bundled-gems`),
RDoc's plugin discovery requires each copy's `lib/rdoc/discover.rb`.
Both unconditionally defined `RDoc::Parser::RBS#scan`, so the second
load redefined the method and emitted:
warning: method redefined; discarding old scan
warning: previous definition of scan was here
Wrap the definition in `unless RDoc::Parser.const_defined?(:RBS, false)`
so the parser is only defined once. `inherit=false` avoids matching a
constant inherited from a superclass.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01McCLCCsB6UajX6PcNsHwND
diff --git a/lib/rdoc/discover.rb b/lib/rdoc/discover.rb
index e0bf2cb..8aae4417 100644
--- a/lib/rdoc/discover.rb
+++ b/lib/rdoc/discover.rb
@@ -3,12 +3,19 @@
begin
gem 'rdoc', '>= 6.16'
require 'rdoc_plugin/parser'
- module RDoc
- class Parser
- class RBS < Parser
- parse_files_matching(/\.rbs$/)
- def scan
- ::RBS::RDocPlugin::Parser.new(@top_level, @content).scan
+ # Guard against defining the parser twice. When two copies of rbs are on the
+ # load path (e.g. an installed gem and a source checkout, as in `ruby/ruby`'s
+ # `test-bundled-gems`), RDoc's plugin discovery requires each `rdoc/discover.rb`,
+ # which would otherwise redefine `RDoc::Parser::RBS#scan` and emit a
+ # "method redefined" warning.
+ unless RDoc::Parser.const_defined?(:RBS, false)
+ module RDoc
+ class Parser
+ class RBS < Parser
+ parse_files_matching(/\.rbs$/)
+ def scan
+ ::RBS::RDocPlugin::Parser.new(@top_level, @content).scan
+ end
end
end
end