class RuboCop::Cop::Style::RedundantFileExtensionInRequire
require_relative ‘../foo.so’
require_relative ‘../foo’
require ‘foo.so’
require ‘foo’
# good
require_relative ‘../foo.rb’
require ‘foo.rb’
# bad
@example
but that seems harmless.
if ‘foo.so` file exists when `require ’foo.rb’‘ will be changed to `require ’foo’‘,
There is an edge case where `foo.so` file is loaded instead of a `LoadError`
a `LoadError` will be raised.
and so on to the name until found. If the file named cannot be found,
Note: If the extension is omitted, Ruby tries adding ’.rb’, ‘.so’,
the filename provided to ‘require` and `require_relative`.
Checks for the presence of superfluous `.rb` extension in
def extension_range(name_node)
def extension_range(name_node) end_of_path_string = name_node.source_range.end_pos range_between(end_of_path_string - 4, end_of_path_string - 1) end
def on_send(node)
def on_send(node) require_call?(node) do |name_node| return unless name_node.value.end_with?('.rb') extension_range = extension_range(name_node) add_offense(extension_range) do |corrector| corrector.remove(extension_range) end end end