Class: Numeric
- Defined in:
- lib/mug/bool.rb,
lib/mug/clamp.rb,
lib/mug/negativity.rb
Instance Method Summary collapse
-
#clamp(lower, higher = nil) ⇒ Object
Clamps num so that lower <= new_num <= higher.
- #negative? ⇒ Boolean
- #nonnegative? ⇒ Boolean
- #nonpositive? ⇒ Boolean
- #positive? ⇒ Boolean
-
#to_b ⇒ Object
Converts num to a boolean.
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
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
5 6 7 |
# File 'lib/mug/negativity.rb', line 5 def negative? self < 0 ? self : nil end |
#nonnegative? ⇒ Boolean
14 15 16 |
# File 'lib/mug/negativity.rb', line 14 def nonnegative? self < 0 ? nil : self end |
#nonpositive? ⇒ Boolean
18 19 20 |
# File 'lib/mug/negativity.rb', line 18 def nonpositive? self > 0 ? nil : self end |
#positive? ⇒ Boolean
9 10 11 |
# File 'lib/mug/negativity.rb', line 9 def positive? self > 0 ? self : nil end |