[Ruby] Method definition location Summary of how to check if it is defined in the project and Rails/Gem

3 minute read

Motivation

When I joined a new project, I had a lot of trouble finding out where the methods were already defined. In order to catch up quickly, I felt that “the ability to quickly investigate and understand” is very important, and I tried to summarize while listening to senior engineers. There are merits and demerits in how to check.

For methods defined in the project

1. git grep -n <method name>

  • It is easier to see because it is displayed in color in the terminal than the full text search in the editor
  • Line numbers are added with the -n option, so open it in the editor by Command + Path click!
  • Disadvantage: String search only. It hits quite a lot. If you use “def method name”, it will be narrowed down a little.

2. Type in the search window on Github in your browser and search for “in this repository”

  • With this method, the result is the same as 1., but several lines above and below the matched part are also displayed together.
  • Good when you don’t want to use an editor
  • Disadvantage: String search only. It hits quite a lot. If you enter “def method name” and double quotation, it will be narrowed down.

3. Use the show-source function of pry-docs

  • In the Rails console, $ <method name> OR show-source <method name>
  • It displays the definition from def to end!
  • Since the path is also output, you can display it in the editor by command + click.
  • Disadvantage: When you want to check user.method, you can only check it after creating a User instance.

4. Use Pry’s “step” function

  • When you stop at binding.pry, you can “enter” the method by executing step.
  • You can see the flow of processing and the definition of variables, and have a deep understanding.
  • You can see deep places such as Rails/Gem.
  • Disadvantage: It is troublesome to install binding.pry and stop processing.

Pika Waka [Rails] A thorough explanation of Pry!

When defined outside the project such as Gem

1. Search with Dash

  • Dash is a tool that can perform cross-search for various documents such as plain Ruby, Rails, Gem, and JavaScript.
  • In addition to the source code, the explanation of the method is included so that understanding will be faster.
  • By adding the extension “Dash” to VS Code, you can jump to Dash search with Command + H
  • Disadvantage: Occasionally there is no gem you want to look up.

2. bundle show <gem name>

  • The directory where the Gem is installed is displayed, so VS Code can be opened in a new window by command + click.
  • Disadvantage: Since git and pry cannot be used, you need to find the method by cross-searching the editor.

3. Use Pry’s “step” function

  • Same as for methods defined in the project.

4. Type in the search window of Github in your browser and search for “in this repository”

  • Same as for methods defined in the project.

Other

1. Use Rubymine’s definer jump

  • I’ve never used it because I’m a VS Code user. However, I feel that Dash can cover almost all definitions in Ruby, Rails, and Gem.
  • Disadvantages: high, slow

2. Using Solargraph, an extension function of VS Code

  • Just put the cursor on the Ruby method, and the document and usage example will be displayed.
  • There is also a complementary function
  • Disadvantage: It is painful to use Docker. Solargraph needs to keep its Language server running. It is necessary to launch the Solargrap container and set the Port setting to VS Code. Reference: How to use solargraph in Docker environment

Finally

  • I would like to know the “how to look” of everyone.
  • If there is another good way, please let us know in the comments m(_ _)m