Answer the question
In order to leave comments, you need to log in
How do gems work in rails?
With this question, I want to understand what gems are and how they work.
For example, in a rails project there is a Gemfile that contains the line
Next, we run the bundle installation command and our project miraculously begins to understand bootstrap classes.
So, so that this miracle does not happen, please help to form a sensible picture of how it works? How do gems work, how does a framework connect to a project, and what does a bundle do in turn?
It's a stupid question, but when you don't have an idea about the collection of different tools that do one thing, it seems difficult to understand. gem 'bootstrap', '~> 4.0.0.beta'
Answer the question
In order to leave comments, you need to log in
About Gem:
Gem is a library, linked in a certain way. That is, it is a set of code (modules, classes, etc.) that solve some problem.
The gem utility is responsible for managing these libraries. For example, 'gem install colorize' will download a library (hereinafter referred to as "gem") from the Internet to a directory available for your programs (the specific location depends on the settings and installation method of Ruby). Then you can write require 'colorize' in your code and use the methods that this gem provides. A gem may require other gems to be installed.
About Bandler:
To avoid installing/updating gems one by one each time, people wrote a Bundler (which is a gem itself). It works like this: in the Gemfile you describe what gems you need (what versions, where to get it, etc...). Then you run bundle install The bundler goes through your Gemfile and installs (using the gem utility) the necessary gems, and also creates the Gemfile.lock file, in which it describes what, where and why it installed. This is an important file! keep it in a repository.
If you say bundle update, then the bundler will look into the Gemfile and Gemfile.lock files, check the versions and install the latest available version of the gem. Attention! not update, but put a new one! That is, the old version will remain.
Here we come to the bundle exec command. This command means to execute something with a gem from the Gemfile.lock file. Attention! everything in the rail (until you get excited) should be run via bundle exec! This will eliminate version conflicts. For example: bundle exec rspec, bundle exec rails db:migrate and so on.
About Rails:
Look in some rails project bin/rails, there is the usual ruby code: require_relative '../config/boot'
We look in config/boot.rb: require 'bundler/setup' Here the Bundler gem is connected and then (you can look at the sources) Bundler.setup is called, which, in turn, looks into the Gemfile and Gemfile.lock files and connects the specified libraries (using require), after which the methods of these libraries become available in the project. By the way, Rail itself is a gem, or rather a set of gems.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question