# Introspection(and its alternatives) in Crystal

**URL:** https://forum.crystal-lang.org/t/introspection-and-its-alternatives-in-crystal/5536
**Category:** Help & Support
**Created:** [April 3, 2023, 10:53pm UTC](https://forum.crystal-lang.org/t/introspection-and-its-alternatives-in-crystal/5536 "2023-04-03T22:53:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ch1c0t](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ch1c0t/32/1947_2.png) [@ch1c0t](https://forum.crystal-lang.org/u/ch1c0t)
#### Post date: [April 3, 2023, 10:53pm UTC](https://forum.crystal-lang.org/t/introspection-and-its-alternatives-in-crystal/5536/1 "2023-04-03T22:53:39Z")

</div>

In Ruby, it is very helpful to use introspection methods(like `#constants`) to discover new APIs.

By analogy, I tried the same in Crystal, with [this example](https://github.com/jhass/crystal-gobject/blob/1e299838bc9c0112a5a16f57021fafd3e31f031d/samples/gtk_accel_group.cr):

```crystal
require "gobject/gtk/autorun"

window = Gtk::ApplicationWindow.new(title: "AccelGroup", border_width: 20)
window.connect "destroy", ->Gtk.main_quit
window.add Gtk::Label.new("Press Ctrl+S")

accel_group = Gtk::AccelGroup.new
accel_group.connect(Gdk::KEY_S, :control_mask, :zero_none) do
  puts "Ctrl+S pressed!"
end
window.add_accel_group(accel_group)

p! Gdk.constants
window.show_all

```

But it failed to compile with:

```auto
| p! Gdk.constants                                               
            ^--------                                               
Error: undefined method 'constants' for Gdk:Module

```

I intended to find out what other combinations besides Ctrl+S(`Gdk::KEY_S`) are there.

I wonder, in similar situations, what do you use for introspection in Crystal?

---

<div class="post-metadata">

### Author: ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)
#### Post date: [April 4, 2023, 12:11am UTC](https://forum.crystal-lang.org/t/introspection-and-its-alternatives-in-crystal/5536/2 "2023-04-04T00:11:13Z")

</div>

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](https://crystal-lang.org/api/master/index.html), 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](https://docs.gtk.org/gdk3/index.html#constants).

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?](https://forum.crystal-lang.org/t/print-out-the-instance-methods-defined-class-or-the-path-where-the-current-class-is/4771)

---

<div class="post-metadata">

### Author: ![ch1c0t](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ch1c0t/32/1947_2.png) [@ch1c0t](https://forum.crystal-lang.org/u/ch1c0t)
#### Post date: [April 4, 2023, 2:07am UTC](https://forum.crystal-lang.org/t/introspection-and-its-alternatives-in-crystal/5536/3 "2023-04-04T02:07:03Z")

</div>

Thank you!
