Module: MoreCoreExtensions::NumericClamp

Defined in:
lib/more_core_extensions/core_ext/numeric/clamp.rb

Instance Method Summary collapse

Instance Method Details

#clamp(min, max) ⇒ Object

Clamp a number to a minimum and/or maximum value.

8.clamp(nil, nil) #=> 8
8.clamp(9, nil)   #=> 9
8.clamp(nil, 6)   #=> 6


9
10
11
12
13
14
# File 'lib/more_core_extensions/core_ext/numeric/clamp.rb', line 9

def clamp(min, max)
  value = self
  value = [value, min].max if min
  value = [value, max].min if max
  value
end