Manage Ruby Gemfile
Gemfile is a collection of Gems and in Ruby program, “Gem is a collection of Ruby codes that we can extract in project for use its functionality (it is like a Library file in C/C++ )”. Gemfile should always be in the root of a project directory, as we can call its methods/actions in our project.
Source of a Gemfile
The first line of Gemfile is Source that means where to look for Gems. We use the #source method for doing this.
It is not recommended to have more than one source per project.
As well as defining a source at the top of our Gemfile we can define a source for each Gem we are loading in. We can also define a path for a local Gem or a Git path for a gem hosted website somewhere like GitHub.
If we declare more than one source and Gem is found in more than one source, it will give us warning explaining which source is used for this Gem.
source 'https://rubygems.org' do gem 'Another gem' gem 'New gem' end
Sources with Credentials
Some sources will require credentials to set. Bundle has a config option that allows us to set a username and password for each source.
bundle config new_gem_source username:password
Syntax in Gemfile
The most basic syntax in Gemfile for Gem is:
gem 'New gem'
Syntax to Add Version in Gem
If we do not set any version in gem, it will install the most updated version of that Gem. For adding version we have several ways to declare:
- Direct Version/Equal to: gem ‘cancan’, ‘1.6.10’
- Not Equal To: gem ‘jbuilder’, ‘!= 2.0’
- Greater Than: gem ‘jbuilder’, ‘> 2.0’
- Less Than: gem ‘jbuilder’, ‘< 2.0’
- Greater Than or Equal To: gem ‘jbuilder’, ‘>= 2.0’
- Less Than or Equal To: gem ‘jbuilder’, ‘<= 2.0’
- Pessimistically Greater Than or Equal To: gem ‘jbuilder’, ‘~>= 2.0’
What is Pessimistically Greater Than or Equal To
The ~> operator allows us to say that any future version of this Gemfile will work with our application.
gem 'jbuilder', '~>= 2.0'
By declaring this is Gemfile we are saying that, any version of 2.X of jbuilder will work in our application but it will not work with another version like 3.x or more.
We can give more restriction by other operators like:
gem 'jbuilder', '>= 2.0',’<=2.5’
A Gem can belong to one or more groups. When it doesn’t belong to any groups that means globally in Gemfile then it is put into the : default group.
group :development, :test do gem 'spring' gem 'capistrano', '2.14.2' gem 'rvm-capistrano', '1.2.7', require: false end
If there is a group that we want to be optional we can pass optional: true before the block;
group :development, optional: true do gem 'spring' gem 'capistrano', '2.14.2' gem 'rvm-capistrano', '1.2.7', require: false end
To install these Gems we have to run:
Bundle install –with development
Setting a Source for Gem
We can explicitly declare the path for installing Gem.
gem 'spring', git: "firstname.lastname@example.org/swawibe/spring"
Include Local Gem with the Path Parameter
We can specify that Gem file is locally on our system by passing in the :path parameter.
gem 'uglifier', :path => "../home/uglifier"
Conditionally Installing Gems
Sometimes we want to only install a gem if some prerequisite is true.
if (Gem.available?('pg')) gem 'pg' else gem 'mysql2' end
Thank you for reading! If you have any feedback, please let us know in a comment.
Contributor: Swawibe Ul Alam, Junior Software Engineer, Nascenia