class RuboCop::Cop::Performance::EndWith


/bc$/.match(‘abc’)
‘abc’.match(/bc$/)
/bc$/ =~ ‘abc’
‘abc’ =~ /bc$/
/bc$/.match?(‘abc’)
‘abc’.match?(/bc$/)
# bad
@example SafeMultiline: false
/bc$/.match(‘abc’)
‘abc’.match(/bc$/)
/bc$/ =~ ‘abc’
‘abc’ =~ /bc$/
/bc$/.match?(‘abc’)
‘abc’.match?(/bc$/)
# good
@example SafeMultiline: true (default)
‘abc’.end_with?(‘bc’)
# good
/bcZ/.match(‘abc’)
‘abc’.match(/bcZ/)
/bcZ/ =~ ‘abc’
‘abc’ =~ /bcZ/
/bcZ/.match?(‘abc’)
‘abc’.match?(/bcZ/)
# bad
@example
of the variables which rubocop doesn’t have.
object. Switching these methods has to be done with knowledge of the types
This will change to a new method call which isn’t guaranteed to be on the
@safety
for receiver is multiline string.
‘end$` is unsafe as it will behave incompatible with `end_with?`
This cop has `SafeMultiline` configuration option that `true` by default because
Identifies unnecessary use of a regex where `String#end_with?` would suffice.

def on_send(node)

def on_send(node)
  return unless (receiver, regex_str = redundant_regex?(node))
  add_offense(node) do |corrector|
    receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
    regex_str = drop_end_metacharacter(regex_str)
    regex_str = interpret_string_escapes(regex_str)
    dot = node.loc.dot ? node.loc.dot.source : '.'
    new_source = "#{receiver.source}#{dot}end_with?(#{to_string_literal(regex_str)})"
    corrector.replace(node, new_source)
  end
end