Manage Ruby Gemfile

November 9, 2015
admin

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.

source 'https://rubygems.org'

It is not recommended to have more than one source per project.

Source Priority

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:

  1. Direct Version/Equal to: gem ‘cancan’, ‘1.6.10’
  2. Not Equal To: gem ‘jbuilder’, ‘!= 2.0’
  3. Greater Than: gem ‘jbuilder’, ‘> 2.0’
  4. Less Than: gem ‘jbuilder’, ‘< 2.0’
  5. Greater Than or Equal To: gem ‘jbuilder’, ‘>= 2.0’
  6. Less Than or Equal To: gem ‘jbuilder’, ‘<= 2.0’
  7. 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.

For example:

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’
Grouping Gem

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: "ssh@githib.com/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 

1 Comment. Leave new

Nice information bro. Thank you

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.