[RUBY] Method definition location Summary of how to check When defined in the project and Rails / Gem
Motivation
When I joined a new project, I had a lot of trouble finding out where it was defined, full of methods. In order to catch up quickly, I felt that "the ability to quickly investigate and understand" was very important, so I asked senior engineers to summarize it.
There are advantages and disadvantages to checking.
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 full-text search in the editor.
- The line number is added with the -n option, so click the command + path to open it in the editor!
- Disadvantages: Character string search. There are quite a lot of hits. If you use "def method name", it will be narrowed down a little.
2. Enter in the search window of Github with a browser and search for "in this repository"
- This method has the same result as 1., but the top and bottom lines of the matched part are also displayed together.
- Good when you don't want to use an editor
- Disadvantages: Character string search. There are quite a lot of hits. It is narrowed down by entering "def method name" and double quotation marks.
3. Use the show-source function of pry-docs
- In the Rails console,
$ <method name>
OR show-source <method name>
- Displays from def to end of the definition part!
- The path is also displayed, so you can display it in the editor by command + click.
- Disadvantage: When you want to check user.method, you have to create a User instance before you can check it, so it is troublesome.
4. Use Pry's "step" feature
- When stopped at binding.pry, you can "enter" into that method by running step.
- You can also see the processing flow and variable definitions for a deeper understanding.
- You can see deep places such as Rails / Gem.
- Disadvantages: It is troublesome to set up binding.pry and stop the process.
Pikawaka [Rails] Pry is thoroughly explained!
When defined outside the project such as Gem
- Dash is a tool that allows cross-searching of various documents such as plain Ruby, Rails, Gem, and JavaScript.
- In addition to the source code, the method is explained so that it is easy to understand.
- By adding the extension "Dash" to VS Code, you can jump to Dash search with command + H.
- Disadvantage: Sometimes there is no Gem you want to check
2. bundle show <gem name>
- Since the directory where Gem is installed is displayed, VS Code can be opened in a new window with command + click.
- Disadvantages: git and pry cannot be used, so you need to find the method by cross-searching in the editor.
3. Use Pry's "step" feature
- Same as for the methods defined in the project.
4. Enter in the search window of Github with a browser and search for "in this repository"
- Same as for the methods defined in the project.
Other
1. Use the Rubymine definition source jump
- I'm a VS Code user, so I've never used it. However, I feel that Dash can almost cover the definitions in Ruby, Rails, and Gem.
- Disadvantages: expensive, slow
2. Use Solargraph, an extension of VS Code
- Simply hover your cursor over a Ruby method to see its documentation and usage examples.
- There is also a complementary function
- Disadvantages: It's painful when using Docker. Solargraph needs to keep its Language server running. You need to launch the Solargrap container and set the Port to VS Code. Reference: How to use solargraph in Docker environment
Finally
- I would like to know your "how to check".
- If there is another good way, please let me know in the comments m (_ _) m