Course of Raku / Objects, I/O, and exceptions / Classes and objects / Class methods / Exercises / Kilometres to miles

Solution: Kilometres to miles

Here is a possible solution to the task.

Code

class Converter {
    method km-to-miles($km) {
        $km * 0.621;
    }
}

say Converter.km-to-miles(10);

🦋 You can find the source code in the file km-to-miles.raku.

Output

6.21

Comments

  1. The conversion does not depend on any particular object, so it is written as a class method and called directly on Converter.

  2. A class method can still take parameters: here it receives the number of kilometres and returns the miles.

Course navigation

Kilometres to miles   |   Quiz — Class methods