How does ruby ​​stone work in rails?

I am trying to understand in rails how ruby gems becomes available for automatic use without required in files that use gems?

+5
source share
3 answers

This is done through bundler/setup : http://bundler.io/v1.3/bundler_setup.html . It is required inside your config/boot.rb . In short, it first sets an environment variable pointing to your Gemfile:

 ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 

Then it adds paths for all your gems to LOAD_PATH, requiring bundler/setup :

 require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) 

Then this requires all the necessary stones (config / application.rb):

 Bundler.require(*Rails.groups) 
+4
source

Rails applications use a connected one (which is used with the Gemfile ). When a packer loads the Gemfile when the rails application starts, it automatically requires all the gems, so you don’t have to do it yourself.

+4
source

I recommend that you read "Rails 4 Craftsmen: Expert Practices for Rails Daily Development" Chapter 1. Creating Your Own Renderer:

Note that the stone has the same name as the file inside the lib directory, which is pdf_renderer. Following this convention, whenever you declare this stone in a Rails Gemfile application, the file in lib / pdf_renderer.rb will be automatically required.

+1
source

Source: https://habr.com/ru/post/1201359/


All Articles