mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-18 10:26:54 +01:00
small clean up
This commit is contained in:
parent
0607e30c31
commit
726cce3875
1 changed files with 36 additions and 52 deletions
|
@ -1,88 +1,72 @@
|
||||||
# A ruby program for temperature conversions
|
# A ruby program for temperature conversions
|
||||||
|
|
||||||
# celsius -> kelvin = value of celsius + 273.15 => K
|
module TemperatureConversion
|
||||||
def celsius_to_kelvin(celsius_input)
|
# celsius -> kelvin = value of celsius + 273.15 => K
|
||||||
begin
|
def self.celsius_to_kelvin(celsius_input)
|
||||||
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
|
|
||||||
puts "#{celsius_input}°C = #{kelvin_output}K"
|
puts "#{celsius_input}°C = #{kelvin_output}K"
|
||||||
|
rescue
|
||||||
|
puts "Error: Please provide number only!"
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# kelvin -> celsius = vale of kelvin - 273.15 => °C
|
# kelvin -> celsius = vale of kelvin - 273.15 => °C
|
||||||
def kelvin_to_celsius(kelvin_input)
|
def self.kelvin_to_celsius(kelvin_input)
|
||||||
begin
|
|
||||||
celsius_output = (kelvin_input - 273.15).round(2)
|
celsius_output = (kelvin_input - 273.15).round(2)
|
||||||
rescue TypeError
|
|
||||||
puts "Error: Please provide number only!"
|
|
||||||
else
|
|
||||||
puts "#{kelvin_input}K = #{celsius_output}°C"
|
puts "#{kelvin_input}K = #{celsius_output}°C"
|
||||||
|
rescue
|
||||||
|
puts "Error: Please provide number only!"
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F
|
# celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F
|
||||||
def celsius_to_fahrenheit(celsius_input)
|
def self.celsius_to_fahrenheit(celsius_input)
|
||||||
begin
|
|
||||||
fahrenheit_output = ((celsius_input * 9 / 5) + 32).round(2)
|
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"
|
puts "#{celsius_input}°C = #{fahrenheit_output}°F"
|
||||||
|
rescue
|
||||||
|
puts "Error: Please provide number only!"
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C
|
# fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C
|
||||||
def fahrenheit_to_celsius(fahrenheit_input)
|
def self.fahrenheit_to_celsius(fahrenheit_input)
|
||||||
begin
|
|
||||||
celsius_output = ((fahrenheit_input - 32) * 5 / 9).round(2)
|
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"
|
puts "#{fahrenheit_input}°F = #{celsius_output}°C"
|
||||||
|
rescue
|
||||||
|
puts "Error: Please provide number only!"
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K
|
# fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K
|
||||||
def fahrenheit_to_kelvin(fahrenheit_input)
|
def self.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 TypeError
|
|
||||||
puts "Error: Please provide number only!"
|
|
||||||
else
|
|
||||||
puts "#{fahrenheit_input}°F = #{kelvin_output}K"
|
puts "#{fahrenheit_input}°F = #{kelvin_output}K"
|
||||||
end
|
rescue
|
||||||
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
|
|
||||||
puts "Error: Please provide number only!"
|
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"
|
puts "#{kelvin_input}K = #{fahrenheit_output}°F"
|
||||||
|
rescue
|
||||||
|
puts "Error: Please provide number only!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# celsius <-> kelvin
|
# celsius <-> kelvin
|
||||||
celsius_to_kelvin(20)
|
TemperatureConversion.celsius_to_kelvin(20)
|
||||||
kelvin_to_celsius(20)
|
TemperatureConversion.kelvin_to_celsius(20)
|
||||||
|
|
||||||
# Invalid input
|
# Invalid input
|
||||||
kelvin_to_celsius("a")
|
TemperatureConversion.kelvin_to_celsius("a")
|
||||||
|
|
||||||
# celsius <-> fahrenheit
|
# celsius <-> fahrenheit
|
||||||
celsius_to_fahrenheit(-20)
|
TemperatureConversion.celsius_to_fahrenheit(-20)
|
||||||
fahrenheit_to_celsius(68)
|
TemperatureConversion.fahrenheit_to_celsius(68)
|
||||||
|
|
||||||
# Invalid input
|
# Invalid input
|
||||||
celsius_to_fahrenheit("abc")
|
TemperatureConversion.celsius_to_fahrenheit("abc")
|
||||||
|
|
||||||
# fahrenheit <-> kelvin
|
# fahrenheit <-> kelvin
|
||||||
fahrenheit_to_kelvin(60)
|
TemperatureConversion.fahrenheit_to_kelvin(60)
|
||||||
kelvin_to_fahrenheit(-60)
|
TemperatureConversion.kelvin_to_fahrenheit(-60)
|
||||||
|
|
||||||
# Invalid input
|
# Invalid input
|
||||||
fahrenheit_to_kelvin("60")
|
TemperatureConversion.fahrenheit_to_kelvin("60")
|
||||||
|
|
Loading…
Reference in a new issue