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.21Comments
The conversion does not depend on any particular object, so it is written as a class method and called directly on
Converter.A class method can still take parameters: here it receives the number of kilometres and returns the miles.