Class: Numeric

Inherits:
Object show all
Defined in:
lib/mug/bool.rb,
lib/mug/clamp.rb,
lib/mug/negativity.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

#negative?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/mug/negativity.rb', line 5

def negative?
	self < 0 ? self : nil
end

#nonnegative?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/mug/negativity.rb', line 14

def nonnegative?
	self < 0 ? nil : self
end

#nonpositive?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/mug/negativity.rb', line 18

def nonpositive?
	self > 0 ? nil : self
end

#positive?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mug/negativity.rb', line 9

def positive?
	self > 0 ? self : nil
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