Class: Numeric

Inherits:
Object show all
Defined in:
lib/flash_extensions/extensions/numeric_extension.rb

Instance Method Summary collapse

Instance Method Details

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/flash_extensions/extensions/numeric_extension.rb', line 4

def multiple_of?(number)
  number != 0 ? modulo(number).zero? : zero?
end

#negative?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/flash_extensions/extensions/numeric_extension.rb', line 9

def negative?
  self < 0
end

#positive?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/flash_extensions/extensions/numeric_extension.rb', line 13

def positive?
  self > 0
end

#to_byte(from = :b, to = :kb) ⇒ Object



17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



68
69
70
71
72
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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