class RuboCop::Cop::Style::ComparableClamp

Enforces the use of ‘Comparable#clamp` instead of comparison by minimum and maximum.

This cop supports autocorrection for ‘if/elsif/else` bad style only. Because `ArgumentError` occurs if the minimum and maximum of `clamp` arguments are reversed. When these are variables, it is not possible to determine which is the minimum and maximum:

source,ruby

[1, [2, 3].max].min # => 1 1.clamp(3, 1) # => min argument must be smaller than max argument (ArgumentError)


@example

# bad
[[x, low].max, high].min

# bad
if x < low
  low
elsif high < x
  high
else
  x
end

# good
x.clamp(low, high)

Constants

MSG
MSG_MIN_MAX
RESTRICT_ON_SEND

Public Instance Methods

on_if(node) click to toggle source
# File lib/rubocop/cop/style/comparable_clamp.rb, line 78
def on_if(node)
  return unless if_elsif_else_condition?(node)

  if_body, elsif_body, else_body = *node.branches

  else_body_source = else_body.source

  if min_condition?(node.condition, else_body_source)
    min = if_body.source
    max = elsif_body.source
  else
    min = elsif_body.source
    max = if_body.source
  end

  prefer = "#{else_body_source}.clamp(#{min}, #{max})"

  add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
    autocorrect(corrector, node, prefer)
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/style/comparable_clamp.rb, line 100
def on_send(node)
  return unless array_min_max?(node)

  add_offense(node, message: MSG_MIN_MAX)
end

Private Instance Methods

autocorrect(corrector, node, prefer) click to toggle source
# File lib/rubocop/cop/style/comparable_clamp.rb, line 108
def autocorrect(corrector, node, prefer)
  if node.elsif?
    corrector.insert_before(node, "else\n")
    corrector.replace(node, "#{indentation(node)}#{prefer}")
  else
    corrector.replace(node, prefer)
  end
end
min_condition?(if_condition, else_body) click to toggle source
# File lib/rubocop/cop/style/comparable_clamp.rb, line 117
def min_condition?(if_condition, else_body)
  lhs, op, rhs = *if_condition

  (lhs.source == else_body && op == :<) || (rhs.source == else_body && op == :>)
end