Class: WeightConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/weight_converter.rb

Constant Summary collapse

@@weights =
{
    "kg": 1000,
    "t": 1_000_000,
    "cent": 100_000,
    "ct": 0.2,
    "g": 1,
    "mg": 0.001,
    "mkg": 0.000001,
    "lot": 1_016_000,
    "sht": 907_180,
    "kip": 453592,
    "st": 6350.29,
    "lb": 453.592,
    "oz": 28.3495,
    "dr": 3.4,
    "gr": 0.065,
    "lbt": 373.24,
    "ozt": 31.1,
    "dwt": 1.55517,
}

Instance Method Summary collapse

Instance Method Details

#colorize(string, color) ⇒ Object



37
38
39
# File 'lib/weight_converter.rb', line 37

def colorize(string, color)
    string.colorize(color)
end

#convert(number, from, to, color) ⇒ Object



25
26
27
# File 'lib/weight_converter.rb', line 25

def convert(number, from, to, color)
    return colorize(convert_g_to(convert_to_g(number.to_f, from), to).to_s, color)
end

#convert_g_to(number, to) ⇒ Object



33
34
35
# File 'lib/weight_converter.rb', line 33

def convert_g_to(number, to)
    return number / @@weights[to] 
end

#convert_to_g(number, from) ⇒ Object



29
30
31
# File 'lib/weight_converter.rb', line 29

def convert_to_g(number, from)
    return number * @@weights[from]  
end