Skip to content

Instantly share code, notes, and snippets.

@Nokogiri
Created August 19, 2019 12:27
Show Gist options
  • Save Nokogiri/6bd13eed71f790a5de07eccd4eaac936 to your computer and use it in GitHub Desktop.
Save Nokogiri/6bd13eed71f790a5de07eccd4eaac936 to your computer and use it in GitHub Desktop.
class Convert
@@temp,@@unit = gets.strip.split
def units
@@unit
end
def temps
self.is_i?(@@temp)
@@temp
end
def is_i?(s)
begin
Integer(s)
rescue ArgumentError
puts 'NaN'
end
end
def is_s?(s)
s.to_s.to_i == s
end
def convert(temp, f)
if f == "C" || f == "c"
return ((temp.to_i * 9/5) +32).to_s + " " + unit_n(units)
elsif f == "F" || f == "f"
return (((temp.to_i - 32) * 5/9).round(2)).to_s + " " + unit_n(units)
else
return 0 #raise 'An error has occured'
end
end
def unit_n(unit)
if @@unit == "F" || @@unit == "f"
return unit2 = "°C"
else
return unit2 = "°F"
end
end
end
x = Convert.new
puts "#{x.temps} °#{x.units} sind #{x.convert(x.temps ,x.units)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment