Tags:
Development

How are gems loaded in a Rails application

You add a new gem to your Gemfile and start using it in your application’s code. You never have to do ‘require <gemname>’ to use it in the application.

Ever wondered how rails autoloads all your gems? Let me walk you through the process which rails follow. Actually Rails uses Bundler to handle dependency management of all the gems and also autoload them.

How Rails put the gems “on the load path”?

[app]/config/boot.rb

The file is loaded during the rails initialization process and all it does is load rubygems and run Bundler. This will automatically discover your Gemfile and puts all the gems listed in the load path so that they can be required later. (Note: The gem’s aren’t run at this point, just the load paths are set.)

How are gems required (run) ?

[app]/config/application.rb

This file includes all the gems listed in the application’s Gemfile by calling Bundler.require. Bundler allows us to specify which groups to automatically require through the parameters to Bundler.require

The above code loads the assets group of gems only in development and test environments. It makes sense as we use precompiled assets in production, hence assets gems aren’t required to be loaded. (see Rails.groups). There are few comments in the file which will help you to change the default behavior if required.

Understanding rails initialization internals allows us to tweak the process to reduce the application load time. We’ll talk more about the process in the coming posts.

Leave a Reply

Your email address will not be published. Required fields are marked *