Course of Raku / Objects, I/O, and exceptions / Classes and objects / The metaobject protocol 🆕 / Exercises / Find a method
Solution: Find a method
Here is a possible solution to the task.
Code
class Animal {
method speak { 'generic' }
}
class Dog is Animal {
}
say so Dog.^find_method('speak');🦋 You can find the source code in the file find-method.raku.
Output
TrueComments
.^find_methodreturns the method if it exists, or an undefined value if not. Thesoturns that into a plain Boolean.Dogdefines no methods itself, yet the result isTrue:find_methodsearches the whole inheritance chain and findsspeakup inAnimal. This mirrors how an actual method call on aDogwould locate the inherited method.