Method: Numeric#crop

Defined in:
lib/extra_lib/core_ext/numeric.rb

#crop(range_or_min, max = nil) ⇒ Object

Credit to apeiros for this method.

Min/max method.

Example: -2.crop(0..1) # => 0

Returns: Numeric



43
44
45
46
47
48
49
50
51
52
# File 'lib/extra_lib/core_ext/numeric.rb', line 43

def crop(range_or_min, max=nil)
  range = max ? range_or_min..max : range_or_min
  if range.include?(self)
    self
  elsif self < range.first
    range.first
  else
    range.last
  end
end