Ruby

Ruby

Made by DeepSource

Use Comparable#clamp to limit value to a range RB-C1019

Anti-pattern
Minor

When you want to limit a value to a certain range, prefer using Comparable#clamp intead of combinations of min/max and conditionals.

Bad practice

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

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

Recommended

x.clamp(low, high)

References

  1. Comparable#clamp