Ruby gem expression - what does it do?

I think I have a basic idea of ​​what to do / include instructions at the top of a ruby ​​script, e.g.

require 'rspec' 

These statements are readily available to Google and find relevant results. But sometimes I saw the gem statement as

 gem 'rspec' 

What does this line do?

+4
source share
3 answers

In the gem(gem_name, *requirements) ruby code gem(gem_name, *requirements) defined in Kernel tells Ruby to load a specific version of gem_name . This is useful if you have installed multiple versions of the same gem.

For example, if you installed two versions of rspec , say 2.12.0 and 2.13.0 , you can call gem before require to use a specific version. Note that gem must appear before calling require .

 gem 'rspec', '=2.12.0' require 'rspec' 

A gem 'gem_name' without a version uses the latest version on your computer, and this is not necessary. You can call require without gem to get the same behavior.

In addition, at Bundler :: Dsl, gem used to tell the vendor about preparing / installing a specific version of ruby ​​gemstones. You will see that in the gemfile

+6
source

The initial behavior of require before Rubygems was to search all the directories listed in the $LOAD_FILES variable for the file and load the first match found. If no matching file is found, require will raise a LoadError value.

Rubygems modifies this process. With Rubygems, require will look for the existing $LOAD_PATH , as before, but if no matching file is found, Rubygems will look for installed stones on your machine to match. If a gem is found containing the corresponding file, this pearl is activated, and then the search for $LOAD_PATH repeated. The main effect of activating the gem is that the lib stones catalog has been added to your download path. Thus, a second search of the download path will find the required file.

Usually this will mean that the latest version of the gem you installed is activated. Sometimes you want to use a different version of the gem, and you can use the gem method for this. The gem method activates the gem, and you can specify the version you need, but do not require any files. When you later request the files you need, you will get them from the gem version you specify.

+4
source

In Ruby, gems are packages with functionality that can be used out of the box (like libraries in other programming languages).

The gems you use with your Ruby project can be easily managed with a tool called bundler, just Google. The code snippet you posted is part of the specification file that the package provider uses to install and update all the libraries that you specified for your project.

If you are developing Ruby on Rails using gems, managing them with bundler is very common and it is therefore recommended that you use best practice.

Gems are simply gorgeous because there are so many useful libraries that extend the default functions, such as rails, and which you can use out of the box!

For a list of gems, visit rubygems.org

-1
source

All Articles