From e2c5fbfb4429e75e00b017cf287a100f18837a07 Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi Date: Thu, 29 Apr 2021 16:36:00 +0530 Subject: [PATCH 1/4] weight conversions feature implementation --- DIRECTORY.md | 1 + conversions/weight_conversions.rb | 99 +++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 conversions/weight_conversions.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 3d98259..bf86d5d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -10,6 +10,7 @@ ## Conversions * [Temperature Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/temperature_conversions.rb) + * [Weight Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/weight_conversions.rb) ## Data Structures * Arrays diff --git a/conversions/weight_conversions.rb b/conversions/weight_conversions.rb new file mode 100644 index 0000000..aa532b3 --- /dev/null +++ b/conversions/weight_conversions.rb @@ -0,0 +1,99 @@ +# A ruby program for weight conversions + +module WeightConversion + # Kilogram -> Gram = (kilogram_value * 1000) grams + def self.kilogram_to_gram(kilogram_input) + if Integer === kilogram_input + gram = kilogram_input * 1000 + "#{kilogram_input} kg = #{gram} g" + else + raise + end + rescue + "Error: Please provide number only!" + end + + # Gram -> Kilogram = (gram_value / 1000) kilograms + def self.gram_to_kilogram(gram_input) + kilogram = gram_input / 1000 + "#{gram_input} g = #{kilogram} kg" + rescue + "Error: Please provide number only!" + end + + # Pound -> Ounce = (pound_value * 16) oz + def self.pound_to_ounce(pound_input) + if Integer === pound_input + ounce = pound_input * 16 + "#{pound_input} lb = #{ounce} oz" + else + raise + end + rescue + "Error: Please provide number only!" + end + + # Ounce -> Pound = (ounce_value / 16) lb + def self.ounce_to_pound(ounce_input) + if Integer === ounce_input + pound = ounce_input / 16 + "#{ounce_input} oz = #{pound} lb" + else + raise + end + rescue + "Error: Please provide number only!" + end + + # Kilogram -> Pound = (kilogram_input * 2.205) lb + def self.kilogram_to_pound(kilogram_input) + if Integer === kilogram_input + pound = (kilogram_input * 2.205).round(2) + "#{kilogram_input} kg = #{pound} lb" + else + raise + end + rescue + "Error: Please provide number only!" + end + + # Pound -> Kilogram = (pound_input / 2.205) kg + def self.pound_to_kilogram(pound_input) + if Integer === pound_input + kilogram = (pound_input / 2.205).round(2) + "#{pound_input} lb = #{kilogram} kg" + else + raise + end + rescue + "Error: Please provide number only!" + end +end + +# Valid inputs +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 + +# Invalid inputs +puts WeightConversion.kilogram_to_gram("a") +# Error: Please provide number only! +puts WeightConversion.gram_to_kilogram("3000") +# Error: Please provide number only! +puts WeightConversion.pound_to_ounce("16") +# Error: Please provide number only! +puts WeightConversion.ounce_to_pound("x ") +# Error: Please provide number only! +puts WeightConversion.kilogram_to_pound("weight") +# Error: Please provide number only! +puts WeightConversion.pound_to_kilogram("100") +# Error: Please provide number only! From 170f90d1310343615b411b14afeef5265effa069 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 1 May 2021 19:40:07 -0700 Subject: [PATCH 2/4] move error handling to the bottom --- conversions/weight_conversions.rb | 108 +++++++++++++++--------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/conversions/weight_conversions.rb b/conversions/weight_conversions.rb index aa532b3..452d932 100644 --- a/conversions/weight_conversions.rb +++ b/conversions/weight_conversions.rb @@ -3,70 +3,47 @@ module WeightConversion # Kilogram -> Gram = (kilogram_value * 1000) grams def self.kilogram_to_gram(kilogram_input) - if Integer === kilogram_input - gram = kilogram_input * 1000 - "#{kilogram_input} kg = #{gram} g" - else - raise - end - rescue - "Error: Please provide number only!" + raise StandardError unless Integer === kilogram_input + + gram = kilogram_input * 1000 + "#{kilogram_input} kg = #{gram} g" end # Gram -> Kilogram = (gram_value / 1000) kilograms def self.gram_to_kilogram(gram_input) kilogram = gram_input / 1000 + "#{gram_input} g = #{kilogram} kg" - rescue - "Error: Please provide number only!" end # Pound -> Ounce = (pound_value * 16) oz def self.pound_to_ounce(pound_input) - if Integer === pound_input - ounce = pound_input * 16 - "#{pound_input} lb = #{ounce} oz" - else - raise - end - rescue - "Error: Please provide number only!" + ounce = pound_input * 16 + + "#{pound_input} lb = #{ounce} oz" end # Ounce -> Pound = (ounce_value / 16) lb def self.ounce_to_pound(ounce_input) - if Integer === ounce_input - pound = ounce_input / 16 - "#{ounce_input} oz = #{pound} lb" - else - raise - end - rescue - "Error: Please provide number only!" + pound = ounce_input / 16 + + "#{ounce_input} oz = #{pound} lb" end # Kilogram -> Pound = (kilogram_input * 2.205) lb def self.kilogram_to_pound(kilogram_input) - if Integer === kilogram_input - pound = (kilogram_input * 2.205).round(2) - "#{kilogram_input} kg = #{pound} lb" - else - raise - end - rescue - "Error: Please provide number only!" + 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) - if Integer === pound_input - kilogram = (pound_input / 2.205).round(2) - "#{pound_input} lb = #{kilogram} kg" - else - raise - end - rescue - "Error: Please provide number only!" + raise StandardError unless Integer === pound_input + + kilogram = (pound_input / 2.205).round(2) + + "#{pound_input} lb = #{kilogram} kg" end end @@ -85,15 +62,38 @@ puts WeightConversion.pound_to_kilogram(100) # 100 lb = 45.35 kg # Invalid inputs -puts WeightConversion.kilogram_to_gram("a") -# Error: Please provide number only! -puts WeightConversion.gram_to_kilogram("3000") -# Error: Please provide number only! -puts WeightConversion.pound_to_ounce("16") -# Error: Please provide number only! -puts WeightConversion.ounce_to_pound("x ") -# Error: Please provide number only! -puts WeightConversion.kilogram_to_pound("weight") -# Error: Please provide number only! -puts WeightConversion.pound_to_kilogram("100") -# Error: Please provide number only! +begin + puts WeightConversion.kilogram_to_gram("a") +rescue StandardError + puts "Error: Please provide number only!" +end + +begin + puts WeightConversion.kilogram_to_gram("3000") +rescue StandardError + puts "Error: Please provide number only!" +end + +begin + puts WeightConversion.kilogram_to_gram("16") +rescue StandardError + puts "Error: Please provide number only!" +end + +begin + puts WeightConversion.kilogram_to_gram("x ") +rescue StandardError + puts "Error: Please provide number only!" +end + +begin + puts WeightConversion.kilogram_to_gram("weight") +rescue StandardError + puts "Error: Please provide number only!" +end + +begin + puts WeightConversion.kilogram_to_gram("100") +rescue StandardError + puts "Error: Please provide number only!" +end From 66672b66ab9b3e538b2c4aa8a6025c8327420f4b Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 1 May 2021 19:40:57 -0700 Subject: [PATCH 3/4] minor changes --- conversions/weight_conversions.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/conversions/weight_conversions.rb b/conversions/weight_conversions.rb index 452d932..72f40a5 100644 --- a/conversions/weight_conversions.rb +++ b/conversions/weight_conversions.rb @@ -6,6 +6,7 @@ module WeightConversion raise StandardError unless Integer === kilogram_input gram = kilogram_input * 1000 + "#{kilogram_input} kg = #{gram} g" end @@ -46,8 +47,10 @@ module WeightConversion "#{pound_input} lb = #{kilogram} kg" end end - +# # Valid inputs +# + puts WeightConversion.kilogram_to_gram(2) # 2 kg = 2000 g puts WeightConversion.gram_to_kilogram(3000) @@ -61,7 +64,10 @@ puts WeightConversion.kilogram_to_pound(1) puts WeightConversion.pound_to_kilogram(100) # 100 lb = 45.35 kg +# # Invalid inputs +# + begin puts WeightConversion.kilogram_to_gram("a") rescue StandardError From fa96729b7250dc592b47b717d4fdd9366bdc87cd Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 1 May 2021 19:42:04 -0700 Subject: [PATCH 4/4] Update weight_conversions.rb --- conversions/weight_conversions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/weight_conversions.rb b/conversions/weight_conversions.rb index 72f40a5..e1f86f1 100644 --- a/conversions/weight_conversions.rb +++ b/conversions/weight_conversions.rb @@ -47,6 +47,7 @@ module WeightConversion "#{pound_input} lb = #{kilogram} kg" end end + # # Valid inputs #