Introspection(and its alternatives) in Crystal

There still is introspection, but it’s not available at runtime. You could try like {% pp Gdk.constants %}. Although the more conventional way to do it (maybe not applicable in this context) is to leverage the generated API docs. This gives you something like Crystal 1.8.0-dev, but for a particular project. These are ideally hosted by the shard to make it easy to explore. However given this particular shard relates to generating bindings to C libs, your best bet would be finding the docs for that lib. E.g. Gdk – 3.0.

EDIT: One thing to keep in mind is .constants will include both actual constants and types within that namespace.

Somewhat related: Print out the instance methods defined class? or the path where the current class is?

3 Likes