Йордан обнови решението на 15.10.2016 10:10 (преди около 8 години)
+def convert_from_c(temp, output_units)
+ if output_units == 'C'
+ temp
+ elsif output_units == 'F'
+ temp * 9.0 / 5 + 32
+ else
+ temp + 273.15
+ end
+end
+
+def convert_from_f(temp, output_units)
+ if output_units == 'F'
+ temp
+ elsif output_units == 'C'
+ (temp - 32) * 5 / 9.0
... или просто (temp - 32) / 1.8
.
+ else
+ (temp + 459.67) * 5 / 9.0
+ end
+end
+
+def convert_from_k(temp, output_units)
+ if output_units == 'K'
+ temp
+ elsif output_units == 'C'
+ temp - 273.15
+ else
+ temp * 9.0 / 5 - 459.67
+ end
+end
+
+def convert_between_temperature_units(temp, input_units, output_units)
+ if input_units == 'C'
+ convert_from_C(temp, output_units)
+ elsif input_units == 'F'
+ convert_from_F(temp, output_units)
+ else
+ convert_from_K(temp, output_units)
+ end
+end
+
+def melting_point_of_substance(substance, units)
+ if substance == 'water'
+ convert_between_temperature_units(0, 'C', units)
+ elsif substance == 'ethanol'
Тук Hash-овете, за които говорихме на последната лекция, биха влезли в много уместна употреба. Така може да си дефинираш двойките "substance" => temperature, и да достъпиш точно температурата на веществото, което ти трябва, вместо да имаш if/elsif/elsif/elsif
+ convert_between_temperature_units(-114, 'C', units)
+ elsif substance == 'gold'
+ convert_between_temperature_units(1064, 'C', units)
+ elsif substance == 'silver'
+ convert_between_temperature_units(961.8, 'C', units)
+ elsif substance == 'copper'
+ convert_between_temperature_units(1085, 'C', units)
+ end
+end
+
+def boiling_point_of_substance(substance, units)
+ if substance == 'water'
+ convert_between_temperature_units(100, 'C', units)
+ elsif substance == 'ethanol'
+ convert_between_temperature_units(78.37, 'C', units)
+ elsif substance == 'gold'
+ convert_between_temperature_units(2700, 'C', units)
+ elsif substance == 'silver'
+ convert_between_temperature_units(2162, 'C', units)
+ elsif substance == 'copper'
+ convert_between_temperature_units(2567, 'C', units)
+ end
+end
... или просто
(temp - 32) / 1.8
.