TheAlgorithms-Ruby/conversions/weight_conversions.rb

107 lines
2.3 KiB
Ruby
Raw Normal View History

# A ruby program for weight conversions
module WeightConversion
# Kilogram -> Gram = (kilogram_value * 1000) grams
def self.kilogram_to_gram(kilogram_input)
2021-09-03 13:24:58 -07:00
raise StandardError unless kilogram_input.is_a?(Integer)
2021-05-01 19:40:07 -07:00
gram = kilogram_input * 1000
2021-05-01 19:40:57 -07:00
2021-05-01 19:40:07 -07:00
"#{kilogram_input} kg = #{gram} g"
end
# Gram -> Kilogram = (gram_value / 1000) kilograms
def self.gram_to_kilogram(gram_input)
kilogram = gram_input / 1000
2021-05-01 19:40:07 -07:00
"#{gram_input} g = #{kilogram} kg"
end
# Pound -> Ounce = (pound_value * 16) oz
def self.pound_to_ounce(pound_input)
2021-05-01 19:40:07 -07:00
ounce = pound_input * 16
"#{pound_input} lb = #{ounce} oz"
end
# Ounce -> Pound = (ounce_value / 16) lb
def self.ounce_to_pound(ounce_input)
2021-05-01 19:40:07 -07:00
pound = ounce_input / 16
"#{ounce_input} oz = #{pound} lb"
end
# Kilogram -> Pound = (kilogram_input * 2.205) lb
def self.kilogram_to_pound(kilogram_input)
2021-05-01 19:40:07 -07:00
pound = (kilogram_input * 2.205).round(2)
"#{kilogram_input} kg = #{pound} lb"
end
# Pound -> Kilogram = (pound_input / 2.205) kg
def self.pound_to_kilogram(pound_input)
2021-09-03 13:24:58 -07:00
raise StandardError unless pound_input.is_a?(Integer)
2021-05-01 19:40:07 -07:00
kilogram = (pound_input / 2.205).round(2)
"#{pound_input} lb = #{kilogram} kg"
end
end
2021-05-01 19:42:04 -07:00
2021-09-03 13:24:58 -07:00
#
# Valid inputs
2021-09-03 13:24:58 -07:00
#
2021-05-01 19:40:57 -07:00
puts WeightConversion.kilogram_to_gram(2)
# 2 kg = 2000 g
puts WeightConversion.gram_to_kilogram(3000)
# 3000 g = 3 kg
puts WeightConversion.pound_to_ounce(16)
# 16 lb = 256 oz
puts WeightConversion.ounce_to_pound(16)
# 16 oz = 1 lb
puts WeightConversion.kilogram_to_pound(1)
# 1 kg = 2.21 lb
puts WeightConversion.pound_to_kilogram(100)
# 100 lb = 45.35 kg
2021-09-03 13:24:58 -07:00
#
# Invalid inputs
2021-09-03 13:24:58 -07:00
#
2021-05-01 19:40:57 -07:00
2021-05-01 19:40:07 -07:00
begin
2021-09-03 13:24:58 -07:00
puts WeightConversion.kilogram_to_gram('a')
2021-05-01 19:40:07 -07:00
rescue StandardError
2021-09-03 13:24:58 -07:00
puts 'Error: Please provide number only!'
2021-05-01 19:40:07 -07:00
end
begin
2021-09-03 13:24:58 -07:00
puts WeightConversion.kilogram_to_gram('3000')
2021-05-01 19:40:07 -07:00
rescue StandardError
2021-09-03 13:24:58 -07:00
puts 'Error: Please provide number only!'
2021-05-01 19:40:07 -07:00
end
begin
2021-09-03 13:24:58 -07:00
puts WeightConversion.kilogram_to_gram('16')
2021-05-01 19:40:07 -07:00
rescue StandardError
2021-09-03 13:24:58 -07:00
puts 'Error: Please provide number only!'
2021-05-01 19:40:07 -07:00
end
begin
2021-09-03 13:24:58 -07:00
puts WeightConversion.kilogram_to_gram('x ')
2021-05-01 19:40:07 -07:00
rescue StandardError
2021-09-03 13:24:58 -07:00
puts 'Error: Please provide number only!'
2021-05-01 19:40:07 -07:00
end
begin
2021-09-03 13:24:58 -07:00
puts WeightConversion.kilogram_to_gram('weight')
2021-05-01 19:40:07 -07:00
rescue StandardError
2021-09-03 13:24:58 -07:00
puts 'Error: Please provide number only!'
2021-05-01 19:40:07 -07:00
end
begin
2021-09-03 13:24:58 -07:00
puts WeightConversion.kilogram_to_gram('100')
2021-05-01 19:40:07 -07:00
rescue StandardError
2021-09-03 13:24:58 -07:00
puts 'Error: Please provide number only!'
2021-05-01 19:40:07 -07:00
end