I’m looking at the code for Rails 3. In rails/cli.rb, which is loaded during the startup process, it makes a call to Rails::ScriptRailsLoader.exec_script_rails! which in turn replaces the current ruby process by reloading the application a second time with a call to exec:
exec RUBY, SCRIPT_RAILS, *ARGV if in_rails_application?
I’m not that familiar with the Rails codebase, so my question is what is the rationale for this? Why doesn’t Rails just continue starting up in the same original process? I’m sure there is a good reason, I just don’t know it.
I think the idea is that ‘rails’ as a command line executable is used in two ways:
For example, outside a rails app:
And inside a rails app:
It looks like
exec_script_rails!is used to pick which interface the rails command presents to the user. This comment and a look at ScriptRailsLoader suggest that the method only calls theexecif the user is inside a rails app – specifically a directory where ‘script/rails’ exists. In that case, the exec replaces the original process with ‘script/rails’ to make these equivalent:Otherwise the script continues on in the same process. So the rationale is that it lets the ‘rails’ command behave in a context-specific way that preserves whatever ‘script/rails’ does.