Reputation: 187
I am trying to understand in rails
, how ruby gems
become available to used automatically without being required
in the files that are using the gems?
Upvotes: 5
Views: 142
Reputation: 44685
This is done through bundler/setup
: http://bundler.io/v1.3/bundler_setup.html. It is required inside your config/boot.rb
file. In short it firstly sets environmental variable to point to your Gemfile:
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
Then it adds paths for all your gems to LOAD_PATH, by requiring bundler/setup
:
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
Then it requires all the needed gems (config/application.rb):
Bundler.require(*Rails.groups)
Upvotes: 4
Reputation: 161
I recomend you to read "Crafting Rails 4 Applications: Expert Practices for Everyday Rails Development" Chapter 1. Creating Our Own Renderer:
Notice the gem has the same name as the file inside the lib directory, which is pdf_renderer . By following this convention, whenever you declare this gem in a Rails application’s Gemfile , the file at lib/pdf_renderer.rb will be automatically required.
Upvotes: 1
Reputation: 55758
Rails applications use bundler (that's the thing using the Gemfile
). When bundler loads the Gemfile
on startup of a rails application, it automatically requires all gems listed there, thus you don't have to do this yourself.
Upvotes: 4