class RuboCop::Cop::Lint::MultipleComparison

In math and Python, we can use ‘x < y < z` style comparison to compare multiple value. However, we can’t use the comparison in Ruby. However, the comparison is not syntax error. This cop checks the bad usage of comparison operators.

@example

# bad
x < y < z
10 <= x <= 20

# good
x < y && y < z
10 <= x && x <= 20

Constants

COMPARISON_METHODS
MSG
RESTRICT_ON_SEND
SET_OPERATION_OPERATORS

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/lint/multiple_comparison.rb, line 33
def on_send(node)
  return unless (center = multiple_compare?(node))
  # It allows multiple comparison using `&`, `|`, and `^` set operation operators.
  # e.g. `x >= y & y < z`
  return if center.send_type? && SET_OPERATION_OPERATORS.include?(center.method_name)

  add_offense(node) do |corrector|
    new_center = "#{center.source} && #{center.source}"

    corrector.replace(center, new_center)
  end
end