class RuboCop::Cop::Performance::EndWith

This cop identifies unnecessary use of a regex where `String#end_with?` would suffice.

@example

@bad
'abc' =~ /bc\Z/
'abc'.match(/bc\Z/)

@good
'abc'.end_with?('bc')

Constants

MSG
SINGLE_QUOTE

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/performance/end_with.rb, line 39
def autocorrect(node)
  redundant_regex?(node) do |receiver, regex_str|
    receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
    regex_str = regex_str[0..-3] # drop \Z anchor
    regex_str = interpret_string_escapes(regex_str)

    lambda do |corrector|
      new_source = receiver.source + '.end_with?(' +
                   to_string_literal(regex_str) + ')'
      corrector.replace(node.source_range, new_source)
    end
  end
end
literal_at_end?(regex_str) click to toggle source
# File lib/rubocop/cop/performance/end_with.rb, line 26
def literal_at_end?(regex_str)
  # is this regexp 'literal' in the sense of only matching literal
  # chars, rather than using metachars like . and * and so on?
  # also, is it anchored at the end of the string?
  regex_str =~ /\A(?:#{LITERAL_REGEX})+\\z\z/
end
on_send(node) click to toggle source
# File lib/rubocop/cop/performance/end_with.rb, line 33
def on_send(node)
  return unless redundant_regex?(node)

  add_offense(node)
end