Method: Numeric#clamp

Defined in:
lib/epitools/core_ext/numbers.rb

#clamp(range) ⇒ Object

Clamp the number to a specific range

Examples:

234234234523.clamp(0..100)   #=> 100
12.clamp(0..100)             #=> 12
-38817112.clamp(0..100)      #=> 0


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/epitools/core_ext/numbers.rb', line 36

def clamp(range)
  if self < range.first
    range.first
  elsif self >= range.last
    if range.exclude_end?
      range.last - 1
    else
      range.last
    end
  else
    self
  end
end