Course of Raku / Objects, I/O, and exceptions / Classes and objects / Methods / Exercises / Temperature conversion
Solution: Temperature conversion
Here is a possible solution to the task.
Code
class Celsius {
has $.degrees;
method to-fahrenheit {
$.degrees * 9 / 5 + 32;
}
}
say Celsius.new(degrees => 100).to-fahrenheit;🦋 You can find the source code in the file temperature.raku.
Output
212Comments
The method works with the object’s own
degreesattribute, so the formula does not need any argument passed in.For
100degrees Celsius, the result is212degrees Fahrenheit.