class Numeric def add(n) self + n end def divide(n) self / n end def multiply(n) self * n end unless method_defined?(:multiple_of?) def multiple_of?(number) number != 0 ? modulo(number).zero? : zero? end end def negative? self < 0 end def positive? self > 0 end def power(n) self ** n end def subtract(n) self - n end 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 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 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 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 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 end