Sometimes it is difficult to find out the source location
of methods by just looking at the class documentation,
as the method could be dynamically defined at many place like in modules, parent classes etc.
That is why ruby provides
to retrieve the source location where a method is defined.
Let’s say we have the following classes:
If we want to find the source location of method
full_name by using
We can easily find out using
It returns the file path and line number where method is defined.
This was only limited to methods though.
We had no way to retrieve the source location
LIMIT constant from our example.
Ruby 2.7 has now added
Module#const_source_location similar to
which returns the source location of a constant.
It returns the file path and line number
where a constant is defined similar to
We can also find the location of
classes and modules using
const_source_location available to call from
In the event the a constant is not defined,
nil is returned.