class Numeric

Public Instance Methods

multiple_of?(number) click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 4
def multiple_of?(number)
  number != 0 ? modulo(number).zero? : zero?
end
negative?() click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 9
def negative?
  self < 0
end
positive?() click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 13
def positive?
  self > 0
end
to_byte(from=:b, to=:kb) click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 17
def to_byte(from=:b, to=:kb)
  scalers = { b: 1, kb: 1024 ** 1, mb: 1024 ** 2, gb: 1024 ** 3, tb: 1024 ** 4, pb: 1024 ** 5, eb: 1024 ** 6 }

  to_f * scalers[from] / scalers[to]
end
to_length(from=:mm, to=:in) click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 23
def to_length(from=:mm, to=:in)
  imperical_scalers = {
    mm: 25.4, cm: 2.54, m: 0.0254, km: 0.0000254,
    in: 1, ft: 12, yd: 36, mi: 63360, nm: 72913.4
  }
  metric_scalers    = {
    mm: 1, cm: 10, m: 1000, km: 1000000,
    in: 0.039370078740157, ft: 0.0032808398950131, yd: 0.0010936132983377, mi: 0.00000062137119223733, nm: 0.00000053995680345572
  }

  case to
  when from
    self
  when :mm, :cm, :m, :km
    if [:in, :ft, :yd, :mi, :nm].include?(from)
      to_f * imperical_scalers[from] * imperical_scalers[to]
    else
      to_f * metric_scalers[from] / metric_scalers[to]
    end
  when :in, :ft, :yd, :mi, :nm
    if [:mm, :cm, :m, :km].include?(from)
      to_f * metric_scalers[from] * metric_scalers[to]
    else
      to_f * imperical_scalers[from] / imperical_scalers[to]
    end
  end
end
to_temperature(from=:c, to=:f) click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 51
def to_temperature(from=:c, to=:f)
  case to
  when from
    self
  when :c, :celsius
    (to_f - 32) * 5 / 9
  when :f, :fahrenheit
    (to_f * 9 / 5) + 32
  when :k, :kelvin
    if [:c, :celsius].include?(from)
      to_f + 273.15
    else
      ((to_f - 32) * 5 / 9) + 273.15
    end
  end
end
to_time_unit(from=:sec, to=:min) click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 68
def to_time_unit(from=:sec, to=:min)
  scalers = { sec: 1, min: 60 ** 1, hr: 60 ** 2, day: (60 ** 2) * 24, yr: (60 ** 2) * 24 * 365 }

  to_f * scalers[from] / scalers[to]
end
to_weight(from=:g, to=:oz) click to toggle source
# File lib/flash_extensions/extensions/numeric_extension.rb, line 74
def to_weight(from=:g, to=:oz)
  imperical_scalers = {
    mg: 28349.523125, cg: 2834.9523125, g: 28.349523125, kg: 0.028349523125, mt: 0.000028349523125,
    oz: 1, lb: 16, tn: 32000
  }
  metric_scalers    = {
    mg: 1, cg: 10, g: 1000, kg: 1000000, mt: 1000000000,
    oz: 0.00003527396194958, lb: 0.0000022046226218488, tn: 0.0000000011023113109244
  }

  case to
  when from
    self
  when :mg, :cg, :g, :kg, :mt
    if [:oz, :lb, :tn].include?(from)
      to_f * imperical_scalers[from] * imperical_scalers[to]
    else
      to_f * metric_scalers[from] / metric_scalers[to]
    end
  when :oz, :lb, :tn
    if [:mg, :cg, :g, :kg, :mt].include?(from)
      to_f * metric_scalers[from] * metric_scalers[to]
    else
      to_f * imperical_scalers[from] / imperical_scalers[to]
    end
  end
end