Course of Raku / Objects, I/O, and exceptions / Classes and objects / Subroutines vs methods / Exercises / Triple as a method
Solution: Triple as a method
Here is a possible solution to the task.
Code
class Number {
has $.n;
method triple {
$.n * 3;
}
}
say Number.new(n => 7).triple;🦋 You can find the source code in the file triple-method.raku.
Output
21Comments
As a method,
triplebelongs to the object and works with the object’s ownn— nothing is passed in.Compared with the subroutine version, the data lives in the object instead of arriving as an argument. Both give
21.