Iterating methods
Iterating methods have a lot in common with iterating types, just with a different macro type. The first thing we need in order to iterate over methods is TypeNode
, representing the type whose methods we are interested in. From there, we can call the #methods
method, which returns ArrayLiteral(Def)
of all the methods defined on that type. For example, let's print an array of all the method names within a class:
abstract class Foo def foo; end end module Bar def bar; end end class Baz < Foo include Bar def baz; end def foo(value : Int32); end def foo(value : String); end def bar(x); end end baz = Baz.new baz.bar 1 baz.bar false {{pp Baz.methods.map &.name}}
Running this would output the following:
[baz, foo, foo, bar]
Notice that similar to the #includers
method, only methods explicitly defined within the type are printed. Also notice that the #foo
method is...