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 Method#source_location
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 Admin
object.
We can easily find out using Method#source_location
.
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
of LIMIT
constant from our example.
Module#const_source_location
Ruby 2.7 has now added Module#const_source_location
similar to Method#source_location
which returns the source location of a constant.
It returns the file path and line number
where a constant is defined similar to Method#source_location
.
We can also find the location of
classes and modules using const_source_location
.
const_source_location
available to call from Object
s.
In the event the a constant is not defined,
nil
is returned.