Until I work with ROR, my basic understanding of Gems were, it is a round shaped Chocolate released by Cadbury confectionery company, which comes in different colors. But Once I encountered ROR, it was something which bugged me on my initial days of learning. What is Ruby Gems, What is Gemfile, What is Source, What is Gemfile.lock, Why we run “bundle install”? So many doubts. For those who encounter a similar situation like me, or needs a brush up, this blog post would be a handy guide,
What is a Gem?
A gem is a packaged Ruby application or library. It has a name (e.g. rake) and a version (e.g. 0.4.16). Gems are managed on your computer using the gem command. You can install, remove, and query (amoung other things) gem packages using the gem command. RubyGems is the name of the project that developed the gem packaging system and the gem command
What is Gemfile and Why we use it?
It holds all the dependencies the projects needs to run smoothly.
What all a GemFile contains?
The following makes the GemFile,
1. Source of the RubyGems Server,
The above line is the always the first line of a GemFile, We need to specify the URL (or) source of the RubyGems server from where various gems required by the project are fetched.
2. Gems and their Versions needed by our Program
We specify all the required Gems along with their versions as mentioned below,
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '~> 3.2.2'
gem 'uglifier', '>= 1.2.3'
So in the above statements, we have mentioned that we need “sass-rails” gems which is of version 3.2.4, and did you observe “~>” and “=>” for coffee-rails and uglifer, Let us look into what are those,
Those Operations are used as Version Constraints,
= Equals version
!= Not equal to version
> Greater than version
< Less than version
>= Greater than or equal to
<= Less than or equal to
~> Approximately greater than
The reason we prefix this to the version numbers is to inform the bundler to install the gem in the specified version range. So you may end up with a question as to where to use “~> ” and “=>”. We use “~>” for gems which undergoes regular minor releases and “=>” which has a timed stable major release.If no version constraint operator is specified, RubyGems will assume that “=” was intended.
3. Gem Grouping
We group gems so that we can tell bundler easily which set of gems to exclude (or) to include. Basically in Rails, the grouping of gems is done with respect to Environments. the three common environments are Development, Testing, Production. Here is the sample snip below we have included “sqlite3″ gem within development group,
group :development do
gem 'sqlite3', '1.3.5'
What is GemFile.lock?
When the bundler installs all the Gems in our system looking from the GemFile, it creates a quick snapshot and writes the versions installed to Gemfile.lock. This way bundler doesn’t have to recalculate all the gem dependencies each time you deploy
What is Bundler?
Bundler is a program for managing gem dependencies in your Ruby projects. With Bundler you can specify which gems your program needs, what versions they should be at, it can help you install them, load them at runtime and distribute them with your software. Basically, it takes all of the guesswork out of installing the gems needed to run your Ruby projects.
To know more on these, refer the following topics,
1. Some of the Problems the Bundler Solves
2. Bundler Site
3. What is Bundler?
5. Optimistic Version Constraint & Pessimistic Version Constraints
It would be great, if you add your suggestions and help links in the comment section below.