Class: Numeric
- Defined in:
- lib/nuggets/numeric/limit.rb,
lib/nuggets/numeric/signum.rb,
lib/nuggets/numeric/duration.rb,
lib/nuggets/numeric/to_multiple.rb
Overview
–
#
A component of ruby-nuggets, some extensions to the Ruby programming # language. #
#
Copyright © 2007-2008 Jens Wille #
#
Authors: #
Jens Wille <jens.wille@uni-koeln.de> #
#
ruby-nuggets is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #
#
ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. #
#
You should have received a copy of the GNU General Public License along # with ruby-nuggets. If not, see <www.gnu.org/licenses/>. #
#
++
Instance Method Summary collapse
-
#ceil_to(target) ⇒ Object
call-seq: num.ceil_to(target) => aNumeric.
-
#floor_to(target) ⇒ Object
call-seq: num.floor_to(target) => aNumeric.
-
#hms ⇒ Object
call-seq: num.hms => anArray.
-
#limit(min, max) ⇒ Object
(also: #between)
call-seq: num.limit(min, max) => aNumeric.
-
#max(max) ⇒ Object
(also: #at_most)
call-seq: num.max(max) => aNumeric.
-
#min(min) ⇒ Object
(also: #at_least)
call-seq: num.min(min) => aNumeric.
- #negative? ⇒ Boolean
- #non_negative? ⇒ Boolean
- #positive? ⇒ Boolean
-
#round_to(target) ⇒ Object
call-seq: num.round_to(target) => aNumeric.
-
#signum ⇒ Object
(also: #sign, #sgn)
call-seq: num.signum => -1, 0, 1.
-
#to_hms(precision = 0, labels = %w[h m s],, time = hms) ⇒ Object
call-seq: num.to_hms(precision = 0, labels = %w[h m s]) => aString.
-
#to_multiple_of(target, what = :round) ⇒ Object
call-seq: num.to_multiple_of(target, what) => aNumeric.
-
#to_ymd(include_hms = false, labels = %w[y m d])) ⇒ Object
call-seq: num.to_ymd(include_hms = false, labels = %w[y m d]) => aString.
-
#ymd ⇒ Object
call-seq: num.ymd => anArray.
Instance Method Details
#ceil_to(target) ⇒ Object
call-seq:
num.ceil_to(target) => aNumeric
Returns the smallest multiple of target greater than or equal to num.
58 59 60 |
# File 'lib/nuggets/numeric/to_multiple.rb', line 58 def ceil_to(target) to_multiple_of(target, :ceil) end |
#floor_to(target) ⇒ Object
call-seq:
num.floor_to(target) => aNumeric
Returns the largest multiple of target less than or equal to num.
50 51 52 |
# File 'lib/nuggets/numeric/to_multiple.rb', line 50 def floor_to(target) to_multiple_of(target, :floor) end |
#hms ⇒ Object
call-seq:
num.hms => anArray
Converts num into hour, minute, and second portions.
34 35 36 37 38 39 40 41 |
# File 'lib/nuggets/numeric/duration.rb', line 34 def hms raise ArgumentError, "negative duration #{self}" if self < 0 one_minute = 60 one_hour = 60 * one_minute [((h,) = divmod(one_hour)).last.divmod(one_minute), h].reverse.flatten # *SCNR* ;-) end |
#limit(min, max) ⇒ Object Also known as: between
call-seq:
num.limit(min, max) => aNumeric
Returns min if that’s larger than num, or max if that’s smaller than num. Otherwise returns num.
35 36 37 38 39 |
# File 'lib/nuggets/numeric/limit.rb', line 35 def limit(min, max) min, max = max, min if max < min self.min(min).max(max) end |
#max(max) ⇒ Object Also known as: at_most
call-seq:
num.max(max) => aNumeric
Returns num or max, whatever is smaller.
57 58 59 |
# File 'lib/nuggets/numeric/limit.rb', line 57 def max(max) self > max ? max : self end |
#min(min) ⇒ Object Also known as: at_least
call-seq:
num.min(min) => aNumeric
Returns num or min, whatever is larger.
47 48 49 |
# File 'lib/nuggets/numeric/limit.rb', line 47 def min(min) self < min ? min : self end |
#negative? ⇒ Boolean
34 35 36 |
# File 'lib/nuggets/numeric/signum.rb', line 34 def negative? self < 0 end |
#non_negative? ⇒ Boolean
38 39 40 |
# File 'lib/nuggets/numeric/signum.rb', line 38 def non_negative? !negative? end |
#positive? ⇒ Boolean
30 31 32 |
# File 'lib/nuggets/numeric/signum.rb', line 30 def positive? self > 0 end |
#round_to(target) ⇒ Object
call-seq:
num.round_to(target) => aNumeric
Rounds num to the nearest multiple of target.
42 43 44 |
# File 'lib/nuggets/numeric/to_multiple.rb', line 42 def round_to(target) to_multiple_of(target, :round) end |
#signum ⇒ Object Also known as: sign, sgn
call-seq:
num.signum => -1, 0, 1
Returns the sign of num.
46 47 48 |
# File 'lib/nuggets/numeric/signum.rb', line 46 def signum positive? ? 1 : negative? ? -1 : 0 end |
#to_hms(precision = 0, labels = %w[h m s],, time = hms) ⇒ Object
call-seq:
num.to_hms(precision = 0, labels = %w[h m s]) => aString
Produces a stringified version of num’s time portions (cf. #hms), with the specified precision for the seconds (treated as floating point). The individual parts are labelled as specified in the labels parameter (hours, minutes, seconds in that order). Leading parts with a value of zero are omitted.
Examples:
180.to_hms #=> "3m0s"
180.75.to_hms #=> "3m1s"
180.75.to_hms(2) #=> "3m0.75s"
8180.to_hms #=> "2h16m20s"
8180.to_hms(0, %w[: :]) #=> "2:16:20"
75 76 77 78 79 80 81 82 |
# File 'lib/nuggets/numeric/duration.rb', line 75 def to_hms(precision = 0, labels = %w[h m s], time = hms) h, m, s = time h.zero? ? m.zero? ? "%0.#{precision}f#{labels[2]}" % [s] : "%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [m, s] : "%d#{labels[0]}%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [h, m, s] end |
#to_multiple_of(target, what = :round) ⇒ Object
call-seq:
num.to_multiple_of(target, what) => aNumeric
Returns the nearest multiple of target according to what.
34 35 36 |
# File 'lib/nuggets/numeric/to_multiple.rb', line 34 def to_multiple_of(target, what = :round) target.zero? ? self : (to_f / target).send(what) * target end |
#to_ymd(include_hms = false, labels = %w[y m d])) ⇒ Object
call-seq:
num.to_ymd(include_hms = false, labels = %w[y m d]) => aString
Produces a stringified version of num’s date portions (cf. #ymd), analogous to #to_hms. Includes time portions (cf. #hms) if include_hms is true.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/nuggets/numeric/duration.rb', line 90 def to_ymd(include_hms = false, labels = %w[y m d]) unless include_hms to_hms(0, labels, ymd) else y, m, d = ymd e = d.truncate "#{to_hms(0, labels, [y, m, e])} #{((d - e) * 24 * 60 * 60).to_hms}" end end |
#ymd ⇒ Object
call-seq:
num.ymd => anArray
Converts num into year, month, and day portions.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/nuggets/numeric/duration.rb', line 47 def ymd raise ArgumentError, "negative duration #{self}" if self < 0 one_day = 24 * 60 * 60 one_month = 30 * one_day one_year = 365.25 * one_day y, m = divmod(one_year) m, d = m.divmod(one_month) [y, m, d / one_day] end |