Overview
Documenting the code reading process for Ruby on Rails.
Preparation
- Create a new project with
rails new RailsCodeReading. - Add the following to the Gemfile:
gem 'pg'
gem 'pry-rails'
gem 'pry-doc'
gem 'pry-byebug'
gem 'byebug'
- Run
bundle config set path '.bundle'and then executebundle install. rails generate controller Example- Add
binding.pryto railties/lib/rails/welcome_controller.rb#L9:
def index
binding.pry
end
Code Reading
Let's explore how WelcomeController#index is invoked.
WelcomeController#index is not defined in config/routes.rb and seems to be defined by default, likely due to the autoload mechanism.
By being autoloaded here, WelcomeController is set in the routing.
Here is the implementation of WelcomeController. railties/lib/rails/welcome_controller.rb#L5
Although I couldn't follow the code in detail, it seems that by leveraging the autoload mechanism, routing is resolved without explicitly registering it.