Gem in Ruby and Rails

dieutb
3 min readJun 19, 2021

In general, Gem is the plugin for Ruby, Rails, Sinatra, etc. In this section, there would be some parts

  1. How to install a gem?
  2. Where is the gem installed and loaded?
  3. How to manage gem? What is the Bundler?
  4. How does the gem load in the main project?
  5. How to load a gem in a single Ruby file?

1. How to install a gem?

  • By default, when a Rail project has been created, it already has a Gemfile to contains all the gems
  • to put a new gem to the project, let place this into the Gemfile
    gem [gem-name], [optional version], [optional require]
  • run command bundle to run the installation
  • After installing, the version of gem has been stored in Gemfile.lock. The Gemfile.lock is not less important than Gemfile, it helps the application know which version should be installed/used for gems

2. Where is a gem installed and loaded?

  • Development environment, the gem’s source would be stored in the ENV[‘GEM_HOME’], ENV[‘GEM_PATH’]. These ENV can be configured in the config.ru of the project or in the .bashrc file of system
  • Production environment, the gem’s source would be stored in the project folder vendor/bundle/ruby/[ruby-version]/gems
  • config.ru
ENV['GEM_HOME']="#{ENV['HOME']}/[path-to-ruby-gem-try-to-load-01]:[try-to-load-02]"
ENV['GEM_PATH']="#{ENV['GEM_HOME']}:[second-path-try]"
require 'rubygems' # load module Gem
Gem.clear_paths # update Gem.paths
  • .bashrc
export GEM_HOME="$HOME/[path-to-ruby-gem-try-to-load-01]:[try-to-load-02]"
export GEM_PATH="$GEM_HOME:[second-path-try]"

3. How to manage gem? What is Bundler?

  • Rails uses Bundler to manage gems
  • Bundler manages Gem’s source, Gem’s dependencies, and Gem’s version
  • the command for the installation gem install bundler
  • to specify the place of gem source to install, put in Gemfile ruby '[ruby-version]'. The ruby version would point out the folder that the gem would be stored when installed in global (development environment)
  • if there is no specific version, Bundler would choose the proper one
  • when updating the version of a gem by changing in the Gemfile and run bundle, Bundler would update the gem and also its dependencies (in a range version allowance specified in Gemfile) if necessary then update Gemfile.lock.
  • bundle update [gem name]would update the gem to the latest version in a range specified allowance. This will resolve dependencies from scratch, ignoring the Gemfile.lock then override the version in the file. So, be careful with this command

4. How does the gem load in the main project?

  • By mechanism, only one file inside the folder lib is loaded by default
  • if there is no designation in Gemfile, the file path would be loaded is lib/[name of gem].rb
  • In case, there is a require: false option, the gem wouldn’t be load but still install Gem and the dependencies of it
  • With the directive require , we also can specify the path to load the default file of Gem. eg: gem ‘rake-cache’, require: ‘rake/cache’. It would look for the file path ‘lib/rake/cache.rb

5. How to load a gem in a single Ruby file?

  • the prerequisite is the Bundler must be installed
  • use a block gemfile to contain the gem that would like to use. The content of the block acts the same way with a Gemfile in Rails.
  • if the gem is not installed before or the version does not exist yet (not a local gem). Bundler would install it in the global environment (ruby gem path)
  • there is no Gemlock for this. It would be better if place a version for each gem
require 'bundler/inline'gemfile do
source 'https://rubygems.org'
gem 'json', require: 'json'
gem 'nap', require: 'rest'
gem 'no-need-to-load-default', require: false
end
puts 'Gems installed and loaded!'
puts "The nap gem is at version #{REST::VERSION}"
puts JSON.parse( {a: 'b'}.to_json )

References

--

--

dieutb

passion in finding solutions and research some things relate to technical and helpful in life. Also strongly interest in foods, travel, and gentle music :)