This article was originally published on bmf-tech.com.
Overview
Recording the code reading work of 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- Implement the index method in the controller
def index
binding.pry
render json: { message: 'Hello World!' }
end
- Set up routing
get "example" => "example#index"
Code Reading
Follow the flow of how Rails processes requests.
Access http://127.0.0.1:3000/example and use pry-backtrace in the console to view the stack trace.
Due to the large output, it's not possible to follow everything, so let's look at it in a condensed form.
- Puma accepts the request and calls the Rails application
-
railties/lib/rails/engine.rb#L536
- Definition of Rack API
-
rack/rack - lib/rack/sendfile.rb#L113
- Rack application is executed
- Resolve routing based on request information
- rails/rails - actionpack/lib/action_dispatch/journey/router.rb#L126
- rails/rails - actionpack/lib/action_dispatch/routing/route_set.rb#L66
- Call the process of the controller matched by routing
- Call the index of the controller
It was more complex than expected, and since I'm not familiar with Ruby, I could only grasp the atmosphere to some extent...
Top comments (0)