Class: Range

Inherits:
Object show all
Defined in:
lib/mug/clamp.rb

Instance Method Summary collapse

Instance Method Details

#bound(val) ⇒ Object

Bounds val so that first <= new_val <= last.

Returns first when val < first, last when val > last, otherwise val itself.

Raises an exception if val >= end and the range is exclusive.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mug/clamp.rb', line 30

def bound val
  a = first
  return a if val < a

  b = last
  if val >= b
    raise ArgumentError, 'greater than or equal to the exclusive range' if exclude_end?
    return b
  end

  val
end