Skip to main content

Ruby Tips - How to figure out where a method is defined

The other day when I was dabbling with the huge codebase, I was wondering where a particular method was being declared for a specific method. I wanted to know this because my greps didn't turn out to volatile and didn't return any results



The Solution

The Ruby interpreter has certain methods that can be used for identifying under which module or class, the particular method is being defined. See the example below.


So using the .source_location you will be able to figure which file it has been declared.

Comments

Featured

Can Pull Requests Talk? Effective Communication Within Developers - An Idea

Majorly, I still love writing code. Do you know why? Because code expresses your intelligence in words that can be processed. And also, it helps others understand what you are trying to communicate in a better way. I believe that writing code is a way of communication and it enables developers understand better than just talking about the concepts or logic in plain english words.

This is the thought process that leads to my today's post.

I came across a situation recently when a Pull Request represented an idea but not an actual implementation. The idea was expressed not in a google document but in a couple of lines, which inherently changes the way the software would work. It meant a big change, but in reality it was not a PR that *has* to be merged, but just something can be written to help others understand what changes it incurs. Mind you, its not a 1000 line change. It was a couple of double-digit line changes.
I would appreciate the developer's smartness in writing the c…
You will receive wonderful short stories written by him and inspirational articles once every month.