Class: Numeric
Instance Method Summary collapse
- #add(n) ⇒ Object
- #divide(n) ⇒ Object
- #multiple_of?(number) ⇒ Boolean
- #multiply(n) ⇒ Object
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #power(n) ⇒ Object
- #subtract(n) ⇒ Object
- #to_byte(from = :b, to = :kb) ⇒ Object
- #to_length(from = :mm, to = :in) ⇒ Object
- #to_temperature(from = :c, to = :f) ⇒ Object
- #to_time_unit(from = :sec, to = :min) ⇒ Object
- #to_weight(from = :g, to = :oz) ⇒ Object
Instance Method Details
#add(n) ⇒ Object
3 4 5 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 3 def add(n) self + n end |
#divide(n) ⇒ Object
7 8 9 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 7 def divide(n) self / n end |
#multiple_of?(number) ⇒ Boolean
16 17 18 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 16 def multiple_of?(number) number != 0 ? modulo(number).zero? : zero? end |
#multiply(n) ⇒ Object
11 12 13 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 11 def multiply(n) self * n end |
#negative? ⇒ Boolean
21 22 23 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 21 def negative? self < 0 end |
#positive? ⇒ Boolean
25 26 27 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 25 def positive? self > 0 end |
#power(n) ⇒ Object
29 30 31 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 29 def power(n) self ** n end |
#subtract(n) ⇒ Object
33 34 35 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 33 def subtract(n) self - n end |
#to_byte(from = :b, to = :kb) ⇒ Object
37 38 39 40 41 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 37 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 43 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 71 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
88 89 90 91 92 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 88 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/nobiru/extensions/numeric_extension.rb', line 94 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 |