Skip to main content

Ruby gem explained

 


What is a Ruby Gem?

Gems are open source libraries just like jar packages in java or modules in Go, the gem contains contain Ruby code. This helps in modularization of the code. A gem can be used by a developer in their own program, without writing that piece of code.

You can check the different gems at https://rubygems.org/. Below are some of the popular gems:

Bundler — Provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.

RSpec — A testing framework that supports BDD for Ruby.

Devise — Devise works with authentication. Website that needs to user log-in’s, Devise handles sign in, sign up, reset password, etc.

JSON — Provides an API for parsing JSON from text.

Nokogiri — Provides HTML, XML, SAX, and Reader parsers with XPath and CSS selector support.

Rails — Rails is a fullstack web application development framework.

Install Gems

To Install gems locally you can run the command:

gem install [gem_name]

The gem install command fetches the code, downloads it to your computer, and installs the gem and any necessary dependencies and builds documentation for the installed gems.

To see all gems installed locally run the command

gem list

Thank you folks.

Most viewed

Ruby on rails part 6 - Blocks , lambda, procs and closure

 Blocks , lambda, procs  and closure Table of content  1. Blocks 2. Lambda 3. Procs 4. Closure Blocks  Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {} 
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments|  block body }  Syntax of block using do-end ['List of items'].each do | block arguments |      # block body end Example of block declared as do-end with each method.   [ 1 , 2 , 3 ].each do |num| puts num end     Output   $ ruby block_with_each.rb 1 2 3 $    Blocks can also be saved in variables or passed as argument to another function.   yield is a Ruby keyword that is used to call a block. When you use the yield keyword, the code inside the block will run. Example of saving a bl