class RuboCop::Cop::Performance::DoubleStartEndWith

This cop checks for double `#start_with?` or `#end_with?` calls separated by `||`. In some cases such calls can be replaced with an single `#start_with?`/`#end_with?` call.

@example

@bad
str.start_with?("a") || str.start_with?(Some::CONST)
str.start_with?("a", "b") || str.start_with?("c")
var1 = ...
var2 = ...
str.end_with?(var1) || str.end_with?(var2)

@good
str.start_with?("a", Some::CONST)
str.start_with?("a", "b", "c")
var1 = ...
var2 = ...
str.end_with?(var1, var2)

Constants

MSG

Public Instance Methods

on_or(node) click to toggle source
# File lib/rubocop/cop/performance/double_start_end_with.rb, line 29
def on_or(node)
  receiver,
  method,
  first_call_args,
  second_call_args = two_start_end_with_calls(node)

  if receiver && second_call_args.all?(&:pure?)
    add_offense(
      node,
      :expression,
      format(
        MSG,
        receiver: receiver.source,
        method: method,
        combined_args: combine_args(first_call_args, second_call_args),
        original_code: node.source
      )
    )
  end
end

Private Instance Methods

combine_args(first_call_args, second_call_args) click to toggle source
# File lib/rubocop/cop/performance/double_start_end_with.rb, line 52
def combine_args(first_call_args, second_call_args)
  (first_call_args + second_call_args).map(&:source).join(', ')
end