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, line 57
def ceil_to(target)
  to_multiple_of(target, :ceil)
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, line 49
def floor_to(target)
   to_multiple_of(target, :floor)
end
hms → anArray click to toggle source

Converts num into hour, minute, and second portions.

# File lib/nuggets/numeric/duration.rb, line 33
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) → 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, line 34
def limit(min, max)
  min, max = max, min if max < min

  min(min).max(max)
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, line 56
def max(max)
  self > max ? max : self
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, line 46
def min(min)
  self < min ? min : self
end
Also aliased as: at_least
negative?() click to toggle source
# File lib/nuggets/numeric/signum.rb, line 33
def negative?
  self < 0
end
non_negative?() click to toggle source
# File lib/nuggets/numeric/signum.rb, line 37
def non_negative?
  !negative?
end
positive?() click to toggle source
# File lib/nuggets/numeric/signum.rb, line 29
def positive?
  self > 0
end
round_to(target) → aNumeric click to toggle source

Rounds num to the nearest multiple of target.

# File lib/nuggets/numeric/to_multiple.rb, line 41
def round_to(target)
   to_multiple_of(target, :round)
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, line 45
def signum
  # http://weblog.jamisbuck.org/2015/8/5/reducing-a-number-to-its-sign.html
  self <=> 0
end
Also aliased as: sign, sgn
signum_s(positive = '+', negative = '-', zero = positive) click to toggle source
# File lib/nuggets/numeric/signum.rb, line 53
def signum_s(positive = '+', negative = '-', zero = positive)
  [zero, positive, negative][signum]
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, line 74
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) → aNumeric click to toggle source

Returns the nearest multiple of target according to what.

# File lib/nuggets/numeric/to_multiple.rb, line 33
def to_multiple_of(target, what = :round)
  target.zero? ? self : (to_f / target).send(what) * target
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, line 89
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 → anArray click to toggle source

Converts num into year, month, and day portions.

# File lib/nuggets/numeric/duration.rb, line 46
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