Class: Numeric

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

Instance Method Summary collapse

Instance Method Details

#clamp(lower, higher = nil) ⇒ Object

Clamps num so that lower <= new_num <= higher.

Returns lower when num < lower, higher when num > higher, otherwise num itself.

Raises an exception if lower > higher

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/mug/clamp.rb', line 12

def clamp lower, higher=nil
	return lower.bound(self) if lower.is_a?(Range) && higher.nil?
	raise ArgumentError, 'range must not be negative' if lower > higher
	[[lower, self].max, higher].min
end

#to_bObject

Converts num to a boolean. Returns true if not zero.



35
36
37
# File 'lib/mug/bool.rb', line 35

def to_b
	self != 0
end