Course of Raku / Objects, I/O, and exceptions / Classes and objects / Methods / Exercises / A reversed word
Solution: A reversed word
Here is a possible solution to the task.
Code
class Word {
has $.text;
method reversed {
$.text.flip;
}
}
say Word.new(text => 'Raku').reversed;🦋 You can find the source code in the file greeter.raku.
Output
ukaRComments
The
reversedmethod reaches the object’s owntextthrough its accessor$.textand calls the built-inflipon it, which returns the string reversed.The method is called directly on the freshly created
Wordobject. Nothing is stored back —reversedsimply computes and returns a new value from the attribute.Note that inside the class, you can read the variable directly without employing the accessor:
method reversed {
$!text.flip;
}