class Numeric

Public Instance Methods

at_least(min)
Alias for: min
at_most(max)
Alias for: max
between(min, max)
Alias for: limit
ceil_to(target) → aNumeric click to toggle source

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
floor_to(target) → aNumeric click to toggle source

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
hms → anArray click to toggle source

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
limit(min, max) → aNumeric click to toggle source

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
Also aliased as: between
max(max) → aNumeric click to toggle source

Returns num or max, whatever is smaller.

   # File lib/nuggets/numeric/limit.rb
56 def max(max)
57   self > max ? max : self
58 end
Also aliased as: at_most
min(min) → aNumeric click to toggle source

Returns num or min, whatever is larger.

   # File lib/nuggets/numeric/limit.rb
46 def min(min)
47   self < min ? min : self
48 end
Also aliased as: at_least
negative?() click to toggle source
   # File lib/nuggets/numeric/signum.rb
33 def negative?
34   self < 0
35 end
non_negative?() click to toggle source
   # File lib/nuggets/numeric/signum.rb
37 def non_negative?
38   !negative?
39 end
positive?() click to toggle source
   # File lib/nuggets/numeric/signum.rb
29 def positive?
30   self > 0
31 end
round_to(target) → aNumeric click to toggle source

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
sgn()
Alias for: signum
sgn_s(positive = '+', negative = '-', zero = positive)
Alias for: signum_s
sign()
Alias for: signum
sign_s(positive = '+', negative = '-', zero = positive)
Alias for: signum_s
signum → -1, 0, 1 click to toggle source

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
Also aliased as: sign, sgn
signum_s(positive = '+', negative = '-', zero = positive) click to toggle source
   # File lib/nuggets/numeric/signum.rb
53 def signum_s(positive = '+', negative = '-', zero = positive)
54   [zero, positive, negative][signum]
55 end
Also aliased as: sign_s, sgn_s
to_hms([precision[, labels]]) → aString click to toggle source

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
to_multiple_of(target, what) → aNumeric click to toggle source

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
to_ymd([include_hms[, labels]]) → aString click to toggle source

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
ymd → anArray click to toggle source

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