Ruby source code navigation

My main goal is to fully understand the library / gem that I use. I tried reading the source code from start to finish on Github, but it was very difficult.

I thought that a nicer and softer step would be to simply read the source code of each library / gem method when I use it.

For example, I want to know how the redirect_to method works in Ruby on Rails:

  • How can I find the source code for the redirect_to method?
    • I know that in pry I can do something like a show-method method , but how to do this for methods in a Rails structure?
  • Do you have any suggestions on how to better understand gems and their APIs? Just reading the source code seems very difficult, especially for frameworks.

Thanks!

+5
source share
3 answers

The RubyMine IDE satisfies this need for me. It has these features that significantly lower the barrier to reading the source of gems:

  • Gems are part of the project tree (in the External Libraries pseudo-directory), so you can navigate and even edit them in the same way as your own code.

  • A single-key command (-B command on a Mac) goes from the identifier (module / class name, method name, etc.) to its declaration or displays a list of choices if there are several declarations. It works for your code as well and for the gem code.

  • Another command (option-command-O on Mac) allows you to enter an identifier name to go to the next, as described above. It automatically includes library code if the code you entered is not found in your code; if what you enter is found in your code, you can press the keys again to enable the gem code.

  • The debugger constantly displays the code that you are executing. You can set a breakpoint without editing the code where you want. You can check any stack stack above the one that is actually running. All these functions work the same way with the code and gem code.

In general, RubyMine has all the functions of working with gem code, as well as project code.

As for methods for understanding gems, other than reading their source code: most gems are documented in their README (which I invariably read on Github) and possibly in other documentation files in the gem root directory or in the doc directory. Some gems (including Rails gems) have an rdoc worth reading. However, since it is so convenient for moving from using a gem to its code, I usually try to do this first and read rdoc (if any) in the code before I read the actual code. RubyMine can also display rdoc for the identifier where it was used (F1 on Mac), but it was always easier for me to just go directly to the code.

+4
source

My first approach when trying to learn how to use a gem is to write tests against it.

Robert C. Martin writes:

Learning third-party code is difficult. It is also difficult to integrate third-party code. Performing both at the same time is doubly difficult. What if we take a different approach? Instead of experimenting and testing new material in our production code, we could write some tests to examine our understanding of third-party code. Jim Newkirk calls such tests training tests.

In training tests, we call the third-party API, since we expect to use it in our application. Essentially doing controlled experiments that tested our understanding of this API. The tests focus on what we want from the API.

Reading the source code occurs after you are really worried about how the internal elements work.

Hope this helps!

As for where redirect_to defined in ActionController :: Redirection

https://github.com/rails/rails/blob/52ce6ece8c8f74064bb64e0a0b1ddd83092718e1/actionpack/lib/action_controller/metal/redirecting.rb

+2
source

If you can get the code in the controller, this is often more informative:

 raise method(:redirect_to).source_location.inspect 

This will show you exactly where the method was defined.

When working with large codebases like this, I usually rely on git pretty hard:

 git grep 'def redirect_to' 

If all else fails:

 git grep 'redirect_to' 

You may need to filter more carefully, but often the implementation stands out visually.

The good thing about Ruby code is, as a rule, quite readable, and searching through it is often more enjoyable than in other languages.

+2
source

All Articles