Aayush
Aayush

Reputation: 1254

Rails initialization process

ENV['RAILS_ENV'] ||= "development"
require File.dirname(__FILE__) + '/../../../config/boot'
require File.join(Rails.root, 'config/environment')

This is the initial part of my rails application. Can anyone explain whats the purpose of these three lines?

Upvotes: 0

Views: 709

Answers (2)

Aayush
Aayush

Reputation: 1254

#!/usr/local/ruby-current/bin/ruby - a shebang comment which tells a Unix like system how to execute this file.

ENV['RAILS_ENV'] ||= "development" - Checks whether the selected environment of work is a development environment. If not it will set the environment to development.

Rails initialization process: In any application's /public directory we can find three files namely dispatch.cgi, dispatch.fcgi, dispatch.rb. The way we configure the server to start up the rails process( as a CGI process, a fast CGI process or a Ruby process) determines which one of the three files will be executed. The dispatch file that's executed will load the Rails environment and respond to requests from the web server by calling the dispatcher. boot.rb ensures that the Rails_Root environment variable has been set. If it hasn't been done boot.rb will define it as the directory one level beneath /config. Once Rails_Root has been set it continues the process of loading up rails by checking the existence of a frozen rails environment in vendor/rails. If this folder exists, then boot.rb will require the Rails initializer from there. If we don't have a local (frozen) copy of Rails, boot.rb will load the rubygems library and scan the environment.rb to see if a RAILS_GEM_VERSION is constant has been defined. If it has boot.rb will load the initializer for that defined version of Rails( and raise an error if that version of Rails does not exist on the system). If RAILS_GEM_VERSION is not defined,boot.rb will attempt to initialize the most recent version of Rails installed on the system.

Upvotes: 1

phoet
phoet

Reputation: 18845

if you want an in depth look of what rails does on startup, head over to the rails guides and read through the initialization chapter:

http://guides.rubyonrails.org/initialization.html

Upvotes: 2

Related Questions