I have drafted this rails initialization process after thorough understanding and debugging rails lib code. I have also referred rails initialisation guide present here - Initialization guide so I tried make it simpler to understand for all of us.
Here is a step by step rails initialization process. (specifically for Rails 3.1.1)
Perform following steps before you start learning and understanding this process
-
git clone https://github.com/rails/rails.git
-
cd rails
-
git checkout v3.1.1
Initialization Process
-
When you do "rails s" from inside your rails app folder, it will load "script/rails.rb" file present in your rails app folder. (This initialization is part of cli.rb present in lib root folder in the source code that we cloned above).
-
The rails.rb script will set APP_PATH and will load config/boot.rb file.
-
config/boot.rb loads rubygems and sets the bundler.
Next, this is bit internal but now the initialization process loads commands.rb from the lib folder (and commands folder has necessary server.rb file to load) Thus it loads config.ru file. So basically rails server is thin wrapper on top of Rack.
-
config.ru file is executed and this in turn starts loading application by first initializing environment.
-
config/environment.rb is now called by the step-4 above. environment.rb loads config file application.rb (check the code inside environment.rb file and you'll understand better) and initializes our app.
-
config/application.rb is now called. This config file loads the boot file if it hasn't loaded already. In our case its already loaded in step-3 above. It then requires "rails/all" which in turn loads entire rails framework. (Internals - this rails/all basically loads all.rb from the lib folder i.e. from the code we cloned above which in turn loads entire rails f/w and railtie pronounced rail tie). So after rails f/w is loaded, the application.rb file will then include all our gems present in Gemfile using bundler. This loads only assets group of gems if we are in dev or test env. This has changed since Rails 3.1 and this it will just load assets group of gems.
The next step in application.rb is creation of our application class. (Internals - refer application.rb -> engine.rb -> railtie.rb in lib folder of our rails source code cloned above.) So a singleton instance of the app is instantiated.
-
config/environments/<specific_env.rb> environment file is then loaded. (Internals - this is basically done at the time of environment config initialization itself but the engine loads it while instantiating the app)
-
config/initializers are now loaded once the app instance is created.
-
App is now started and routes are loaded.
Please let me know in case anything needs correction or any step is missed.