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

212

Comments

  1. The method works with the object’s own degrees attribute, so the formula does not need any argument passed in.

  2. For 100 degrees Celsius, the result is 212 degrees Fahrenheit.

Course navigation

Temperature conversion   |   Class methods