class RuboCop::Cop::Performance::DoubleStartEndWith
str.end_with?(var1, var2)
var2 = …
var1 = …
str.start_with?(“a”, “b”, “c”)
str.start_with?(“a”, Some::CONST)
@good
str.end_with?(var1) || str.end_with?(var2)
var2 = …
var1 = …
str.start_with?(“a”, “b”) || str.start_with?(“c”)
str.start_with?(“a”) || str.start_with?(Some::CONST)
@bad
@example
with an single ‘#start_with?`/`#end_with?` call.
separated by `||`. In some cases such calls can be replaced
This cop checks for double `#start_with?` or `#end_with?` calls
def combine_args(first_call_args, second_call_args)
def combine_args(first_call_args, second_call_args) (first_call_args + second_call_args).map(&:source).join(', ') end
def on_or(node)
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