We looked at ways of inspecting methods by opening up their object forms and peeking into classes.
allows us another form of inspection, not entirely unlike
; using constant names as a
It is often used to instantiate classes at runtime, like creating an object of a class for a particular user input. If you're familiar with Rails
, then you might have seen this used for instantiating controllers depending on a certain URL being requested. As class names are constants too, it is really easy to do that.
Because this is just working with strings that are assumed to be constant names, it would not work if your classes aren't named
is defined on
, but it works for classes as well, as
is a subclass of
. It returns a
if the value of that constant isn't found. This is the only way you can access nested constants, as
only parses properly formed named
constants and does not parse colons like
This also restricts the scope for the constant search to the class
. If it doesn't find the constant in that scope it goes up the ancestral chain and starts looking from
isn't exactly safe. Modify the
exercise from the last lesson to use
to parse the name of the class in the second filter.