Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/casual_support/numeric/constrain.rb,
lib/casual_support/numeric/at_least_most.rb

Instance Method Summary collapse

Instance Method Details

#at_least(limit) ⇒ Numeric

Enforces a lower bound for a number.

Parameters:

Returns:

  • (Numeric)

    the number constrained by the lower bound



7
8
9
# File 'lib/casual_support/numeric/at_least_most.rb', line 7

def at_least(limit)
  self < limit ? limit : self
end

#at_most(limit) ⇒ Numeric Also known as: cap

Enforces an upper bound for a number.

Parameters:

Returns:

  • (Numeric)

    the number constrained by the upper bound



15
16
17
# File 'lib/casual_support/numeric/at_least_most.rb', line 15

def at_most(limit)
  self > limit ? limit : self
end

#constrain(low, high) ⇒ Numeric Also known as: clamp

Constrains a number to a closed interval.

Parameters:

Returns:

  • (Numeric)

    the number constrained to the bounds



8
9
10
11
12
# File 'lib/casual_support/numeric/constrain.rb', line 8

def constrain(low, high)
  return low if self < low
  return high if self > high
  self
end