title: How does Rails set the root directory during initialization?
body: <p>There are two blog posts and a rails cast on how Rails creates an application, but neither explains how Rails finds and sets its own root-directory.</p>
  
  <ul>
  <li><p><a href="http://climber2002.github.io/blog/2014/08/24/digging-rails-how-rails-initializes-itself/" rel="nofollow noreferrer">http://climber2002.github.io/blog/2014/08/24/digging-rails-how-rails-initializes-itself/</a></p></li>
  <li><p><a href="http://railscasts.com/episodes/299-rails-initialization-walkthrough?view=asciicast" rel="nofollow noreferrer">http://railscasts.com/episodes/299-rails-initialization-walkthrough?view=asciicast</a></p></li>
  </ul>
  
  <p>What they do explain is how, through an <code>inherited</code> hook mainly, the class of the application is set and how it is then instantiated via delegation by the <code>#initialize</code>-method.</p>
  
  <p>What they largely omit to my regret, however, is how Rails finds and sets its root directory, which I need in order to instantiate the application: <code>/Engine</code> and <code>/Configuration</code>-classes.</p>
  
  <p>For example, in the first blog post, <code>Rails.application.root</code> is already there:</p>
  
  <pre><code>    Rails::Server.new.tap do |server|
      require APP_PATH
      Dir.chdir(Rails.application.root)
      server.start
  </code></pre>
  
  <p>Where and how does Rails set its root-directory?</p>