class Numeric
Public Instance Methods
Returns the smallest multiple of target
greater than or equal to num.
# File lib/nuggets/numeric/to_multiple.rb 57 def ceil_to(target) 58 to_multiple_of(target, :ceil) 59 end
Returns the largest multiple of target
less than or equal to num.
# File lib/nuggets/numeric/to_multiple.rb 49 def floor_to(target) 50 to_multiple_of(target, :floor) 51 end
Converts num into hour, minute, and second portions.
# File lib/nuggets/numeric/duration.rb 33 def hms 34 raise ::ArgumentError, "negative duration #{self}" if self < 0 35 36 one_minute = 60 37 one_hour = 60 * one_minute 38 39 [((h,) = divmod(one_hour)).last.divmod(one_minute), h].reverse.flatten # *SCNR* ;-) 40 end
Returns min
if that's larger than num, or max
if that's smaller than num. Otherwise returns num.
# File lib/nuggets/numeric/limit.rb 34 def limit(min, max) 35 min, max = max, min if max < min 36 37 min(min).max(max) 38 end
Returns num or max
, whatever is smaller.
# File lib/nuggets/numeric/limit.rb 56 def max(max) 57 self > max ? max : self 58 end
Returns num or min
, whatever is larger.
# File lib/nuggets/numeric/limit.rb 46 def min(min) 47 self < min ? min : self 48 end
# File lib/nuggets/numeric/signum.rb 33 def negative? 34 self < 0 35 end
# File lib/nuggets/numeric/signum.rb 37 def non_negative? 38 !negative? 39 end
# File lib/nuggets/numeric/signum.rb 29 def positive? 30 self > 0 31 end
Rounds num to the nearest multiple of target
.
# File lib/nuggets/numeric/to_multiple.rb 41 def round_to(target) 42 to_multiple_of(target, :round) 43 end
Returns the sign of num.
# File lib/nuggets/numeric/signum.rb 45 def signum 46 # http://weblog.jamisbuck.org/2015/8/5/reducing-a-number-to-its-sign.html 47 self <=> 0 48 end
# File lib/nuggets/numeric/signum.rb 53 def signum_s(positive = '+', negative = '-', zero = positive) 54 [zero, positive, negative][signum] 55 end
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"
# File lib/nuggets/numeric/duration.rb 74 def to_hms(precision = 0, labels = %w[h m s], time = hms) 75 h, m, s = time 76 77 h.zero? ? m.zero? ? 78 "%0.#{precision}f#{labels[2]}" % [s] : 79 "%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [m, s] : 80 "%d#{labels[0]}%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [h, m, s] 81 end
Returns the nearest multiple of target
according to what
.
# File lib/nuggets/numeric/to_multiple.rb 33 def to_multiple_of(target, what = :round) 34 target.zero? ? self : (to_f / target).send(what) * target 35 end
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
.
# File lib/nuggets/numeric/duration.rb 89 def to_ymd(include_hms = false, labels = %w[y m d]) 90 unless include_hms 91 to_hms(0, labels, ymd) 92 else 93 y, m, d = ymd 94 e = d.truncate 95 96 "#{to_hms(0, labels, [y, m, e])} #{((d - e) * 24 * 60 * 60).to_hms}" 97 end 98 end
Converts num into year, month, and day portions.
# File lib/nuggets/numeric/duration.rb 46 def ymd 47 raise ::ArgumentError, "negative duration #{self}" if self < 0 48 49 one_day = 24 * 60 * 60 50 one_month = 30 * one_day 51 one_year = 365.25 * one_day 52 53 y, m = divmod(one_year) 54 m, d = m.divmod(one_month) 55 56 [y, m, d / one_day] 57 end