Мая обнови решението на 12.10.2016 17:41 (преди около 8 години)
+SUBSTANCE_TEMPERATURES = [
+ { name: 'water', melting_point: 0, boiling_point: 100 },
+ { name: 'ethanol', melting_point: -114, boiling_point: 78.37 },
+ { name: 'gold', melting_point: 1064, boiling_point: 2700 },
+ { name: 'silver', melting_point: 961.8, boiling_point: 2162 },
+ { name: 'copper', melting_point: 1085, boiling_point: 2567 }
+].freeze
+
+def convert_between_temperature_units(degrees, from, to)
+ case from
+ when 'C' then from_celsius(degrees, to)
+ when 'K' then from_kelvin(degrees, to)
+ else
+ from_fahrenheit(degrees, to)
+ end
+end
+
+def melting_point_of_substance(name, unit)
+ from_celsius(
+ SUBSTANCE_TEMPERATURES
+ .select { |substance| substance[:name] == name }
+ .first[:melting_point],
+ unit
+ )
+end
+
+def boiling_point_of_substance(name, unit)
+ from_celsius(
+ SUBSTANCE_TEMPERATURES
+ .select { |substance| substance[:name] == name }
+ .first[:boiling_point],
+ unit
+ )
+end
+
+def from_celsius(degrees, to)
+ case to
+ when 'C' then degrees
+ when 'F' then degrees * 1.8 + 32
+ when 'K' then degrees + 273.15
+ end
+end
+
+def from_fahrenheit(degrees, to)
+ case to
+ when 'F' then degrees
+ when 'C' then (degrees - 32) / 1.8
+ when 'K' then (degrees + 459.67) / 1.8
+ end
+end
+
+def from_kelvin(degrees, to)
+ case to
+ when 'K' then degrees
+ when 'C' then degrees - 273.15
+ when 'F' then degrees * 1.8 - 459.67
+ end
+end