From b180da651117bea0393a26152cefd49e09b7ee42 Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi <msafarookhi@gmail.com> Date: Wed, 31 Mar 2021 15:23:16 +0530 Subject: [PATCH 1/4] temperature conversions: ruby program implementation --- conversions/temperature_conversions.rb | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 conversions/temperature_conversions.rb diff --git a/conversions/temperature_conversions.rb b/conversions/temperature_conversions.rb new file mode 100644 index 0000000..b589548 --- /dev/null +++ b/conversions/temperature_conversions.rb @@ -0,0 +1,88 @@ +# A ruby program for temperature conversions + +# celsius -> kelvin = value of celsius + 273.15 => K +def celsius_to_kelvin(celsius_input) + begin + kelvin_output = (celsius_input + 273.15).round(2); + rescue TypeError + puts "Error: Please provide number only!" + else + puts "#{celsius_input}°C = #{kelvin_output}K" + end +end + +# kelvin -> celsius = vale of kelvin - 273.15 => °C +def kelvin_to_celsius(kelvin_input) + begin + celsius_output = (kelvin_input - 273.15).round(2); + rescue + puts "Error: Please provide number only!" + else + puts "#{kelvin_input}K = #{celsius_output}°C" + end +end + +# celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F +def celsius_to_fahrenheit(celsius_input) + begin + fahrenheit_output = ((celsius_input * 9 / 5) + 32).round(2); + rescue + puts "Error: Please provide number only!" + else + puts "#{celsius_input}°C = #{fahrenheit_output}°F" + end +end + +# fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C +def fahrenheit_to_celsius(fahrenheit_input) + begin + celsius_output = ((fahrenheit_input - 32) * 5 / 9).round(2); + rescue + puts "Error: Please provide number only!" + else + puts "#{fahrenheit_input}°F = #{celsius_output}°C" + end +end + +# fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K +def fahrenheit_to_kelvin(fahrenheit_input) + begin + kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2); + rescue + puts "Error: Please provide number only!" + else + puts "#{fahrenheit_input}°F = #{kelvin_output}K" + end +end + +# kelvin -> fahrenheit = [(value of kelvin - 32) * 5 / 9] + 273.15 => K +def kelvin_to_fahrenheit(kelvin_input) + begin + fahrenheit_output = (((kelvin_input - 273.15) * 9 / 5) + 32).round(2).round(2); + rescue + puts "Error: Please provide number only!" + else + puts "#{kelvin_input}K = #{fahrenheit_output}°F" + end +end + +# celsius <-> kelvin +celsius_to_kelvin(20) +kelvin_to_celsius(20) + +# Invalid input +kelvin_to_celsius("a") + +# celsius <-> fahrenheit +celsius_to_fahrenheit(-20) +fahrenheit_to_celsius(68) + +# Invalid input +celsius_to_fahrenheit("abc") + +# fahrenheit <-> kelvin +fahrenheit_to_kelvin(60) +kelvin_to_fahrenheit(-60) + +# Invalid input +fahrenheit_to_kelvin("60") From 381b66bed392a7235279131ef1d2c17fe9761a5e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira <vbrazo@gmail.com> Date: Wed, 31 Mar 2021 18:48:05 -0700 Subject: [PATCH 2/4] Update conversions/temperature_conversions.rb --- conversions/temperature_conversions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/temperature_conversions.rb b/conversions/temperature_conversions.rb index b589548..a0325c4 100644 --- a/conversions/temperature_conversions.rb +++ b/conversions/temperature_conversions.rb @@ -47,7 +47,7 @@ end # fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K def fahrenheit_to_kelvin(fahrenheit_input) begin - kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2); + kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2) rescue puts "Error: Please provide number only!" else From 0607e30c314e8d4bc5925ed36ec9f845fd710076 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira <vbrazo@gmail.com> Date: Wed, 31 Mar 2021 18:49:42 -0700 Subject: [PATCH 3/4] Update temperature_conversions.rb --- conversions/temperature_conversions.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/conversions/temperature_conversions.rb b/conversions/temperature_conversions.rb index a0325c4..c92f083 100644 --- a/conversions/temperature_conversions.rb +++ b/conversions/temperature_conversions.rb @@ -3,7 +3,7 @@ # celsius -> kelvin = value of celsius + 273.15 => K def celsius_to_kelvin(celsius_input) begin - kelvin_output = (celsius_input + 273.15).round(2); + kelvin_output = (celsius_input + 273.15).round(2) rescue TypeError puts "Error: Please provide number only!" else @@ -14,8 +14,8 @@ end # kelvin -> celsius = vale of kelvin - 273.15 => °C def kelvin_to_celsius(kelvin_input) begin - celsius_output = (kelvin_input - 273.15).round(2); - rescue + celsius_output = (kelvin_input - 273.15).round(2) + rescue TypeError puts "Error: Please provide number only!" else puts "#{kelvin_input}K = #{celsius_output}°C" @@ -25,8 +25,8 @@ end # celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F def celsius_to_fahrenheit(celsius_input) begin - fahrenheit_output = ((celsius_input * 9 / 5) + 32).round(2); - rescue + fahrenheit_output = ((celsius_input * 9 / 5) + 32).round(2) + rescue TypeError puts "Error: Please provide number only!" else puts "#{celsius_input}°C = #{fahrenheit_output}°F" @@ -36,8 +36,8 @@ end # fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C def fahrenheit_to_celsius(fahrenheit_input) begin - celsius_output = ((fahrenheit_input - 32) * 5 / 9).round(2); - rescue + celsius_output = ((fahrenheit_input - 32) * 5 / 9).round(2) + rescue TypeError puts "Error: Please provide number only!" else puts "#{fahrenheit_input}°F = #{celsius_output}°C" @@ -48,7 +48,7 @@ end def fahrenheit_to_kelvin(fahrenheit_input) begin kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2) - rescue + rescue TypeError puts "Error: Please provide number only!" else puts "#{fahrenheit_input}°F = #{kelvin_output}K" @@ -58,8 +58,8 @@ end # kelvin -> fahrenheit = [(value of kelvin - 32) * 5 / 9] + 273.15 => K def kelvin_to_fahrenheit(kelvin_input) begin - fahrenheit_output = (((kelvin_input - 273.15) * 9 / 5) + 32).round(2).round(2); - rescue + fahrenheit_output = (((kelvin_input - 273.15) * 9 / 5) + 32).round(2).round(2) + rescue TypeError puts "Error: Please provide number only!" else puts "#{kelvin_input}K = #{fahrenheit_output}°F" From 726cce387547a8ffd4d3fc16b2d793a5ca8bca93 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira <vbrazo@gmail.com> Date: Wed, 31 Mar 2021 18:55:38 -0700 Subject: [PATCH 4/4] small clean up --- conversions/temperature_conversions.rb | 88 +++++++++++--------------- 1 file changed, 36 insertions(+), 52 deletions(-) diff --git a/conversions/temperature_conversions.rb b/conversions/temperature_conversions.rb index c92f083..d62b807 100644 --- a/conversions/temperature_conversions.rb +++ b/conversions/temperature_conversions.rb @@ -1,88 +1,72 @@ # A ruby program for temperature conversions -# celsius -> kelvin = value of celsius + 273.15 => K -def celsius_to_kelvin(celsius_input) - begin +module TemperatureConversion + # celsius -> kelvin = value of celsius + 273.15 => K + def self.celsius_to_kelvin(celsius_input) kelvin_output = (celsius_input + 273.15).round(2) - rescue TypeError - puts "Error: Please provide number only!" - else puts "#{celsius_input}°C = #{kelvin_output}K" + rescue + puts "Error: Please provide number only!" end -end -# kelvin -> celsius = vale of kelvin - 273.15 => °C -def kelvin_to_celsius(kelvin_input) - begin + # kelvin -> celsius = vale of kelvin - 273.15 => °C + def self.kelvin_to_celsius(kelvin_input) celsius_output = (kelvin_input - 273.15).round(2) - rescue TypeError - puts "Error: Please provide number only!" - else puts "#{kelvin_input}K = #{celsius_output}°C" + rescue + puts "Error: Please provide number only!" end -end -# celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F -def celsius_to_fahrenheit(celsius_input) - begin + # celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F + def self.celsius_to_fahrenheit(celsius_input) fahrenheit_output = ((celsius_input * 9 / 5) + 32).round(2) - rescue TypeError - puts "Error: Please provide number only!" - else puts "#{celsius_input}°C = #{fahrenheit_output}°F" + rescue + puts "Error: Please provide number only!" end -end -# fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C -def fahrenheit_to_celsius(fahrenheit_input) - begin + # fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C + def self.fahrenheit_to_celsius(fahrenheit_input) celsius_output = ((fahrenheit_input - 32) * 5 / 9).round(2) - rescue TypeError - puts "Error: Please provide number only!" - else puts "#{fahrenheit_input}°F = #{celsius_output}°C" + rescue + puts "Error: Please provide number only!" end -end -# fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K -def fahrenheit_to_kelvin(fahrenheit_input) - begin + # fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K + def self.fahrenheit_to_kelvin(fahrenheit_input) kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2) - rescue TypeError - puts "Error: Please provide number only!" - else puts "#{fahrenheit_input}°F = #{kelvin_output}K" - end -end - -# kelvin -> fahrenheit = [(value of kelvin - 32) * 5 / 9] + 273.15 => K -def kelvin_to_fahrenheit(kelvin_input) - begin - fahrenheit_output = (((kelvin_input - 273.15) * 9 / 5) + 32).round(2).round(2) - rescue TypeError + rescue puts "Error: Please provide number only!" - else + end + + # kelvin -> fahrenheit = [(value of kelvin - 32) * 5 / 9] + 273.15 => K + def self.kelvin_to_fahrenheit(kelvin_input) + fahrenheit_output = (((kelvin_input - 273.15) * 9 / 5) + 32).round(2).round(2) puts "#{kelvin_input}K = #{fahrenheit_output}°F" + rescue + puts "Error: Please provide number only!" end end # celsius <-> kelvin -celsius_to_kelvin(20) -kelvin_to_celsius(20) +TemperatureConversion.celsius_to_kelvin(20) +TemperatureConversion.kelvin_to_celsius(20) # Invalid input -kelvin_to_celsius("a") +TemperatureConversion.kelvin_to_celsius("a") # celsius <-> fahrenheit -celsius_to_fahrenheit(-20) -fahrenheit_to_celsius(68) +TemperatureConversion.celsius_to_fahrenheit(-20) +TemperatureConversion.fahrenheit_to_celsius(68) # Invalid input -celsius_to_fahrenheit("abc") +TemperatureConversion.celsius_to_fahrenheit("abc") # fahrenheit <-> kelvin -fahrenheit_to_kelvin(60) -kelvin_to_fahrenheit(-60) +TemperatureConversion.fahrenheit_to_kelvin(60) +TemperatureConversion.kelvin_to_fahrenheit(-60) # Invalid input -fahrenheit_to_kelvin("60") +TemperatureConversion.fahrenheit_to_kelvin("60")