Ruby Gems

Reusable Ruby libraries and packages

💎 What are Ruby Gems?

Ruby Gems are packaged Ruby libraries that extend functionality. They provide reusable code for common tasks, making development faster and easier. Install gems using the gem command to add powerful features to your projects.


# Install a gem from command line
gem install colorize

# Use the gem in your code
require 'colorize'
puts "Hello World!".colorize(:red)
                                    

Output:

Hello World!

Key Gem Concepts

📦

Installation

Install gems with gem command

gem install gem_name
🔍

Search

Find gems on RubyGems.org

gem search json
📋

List

View installed gems

gem list
🗑️

Uninstall

Remove gems you don't need

gem uninstall gem_name

🔹 Installing Gems

Gems are installed using the gem command from your terminal. Once installed, you can require them in your Ruby programs to access their functionality and features.

# Install a single gem
gem install httparty

# Install a specific version
gem install rails -v 7.0.0

# Install without documentation (faster)
gem install nokogiri --no-document

Output:

Successfully installed httparty-0.21.0
1 gem installed

🔹 Using Gems in Code

After installing a gem, use the require statement to load it into your Ruby program. Then you can access all the classes, methods, and features provided by that gem.

# Require the gem
require 'httparty'

# Use the gem's functionality
response = HTTParty.get('https://api.github.com/users/github')
puts response['name']
puts response['public_repos']

Output:

GitHub
123

🔹 Popular Ruby Gems

The Ruby community has created thousands of gems for various purposes. Here are some popular gems that beginners often use for web development, testing, and data processing tasks.

Common Gems:

  • rails - Web application framework
  • rspec - Testing framework
  • pry - Debugging tool
  • nokogiri - HTML/XML parser
  • devise - Authentication solution
# Example: Using nokogiri to parse HTML
require 'nokogiri'
require 'open-uri'

html = '

Hello

' doc = Nokogiri::HTML(html) puts doc.css('h1').text

Output:

Hello

🔹 Managing Gem Versions

You can check installed gem versions, update gems to newer releases, and manage multiple versions of the same gem on your system for different projects.

# Check gem version
gem list rails

# Update a gem
gem update rails

# Update all gems
gem update

# Check outdated gems
gem outdated

Output:

rails (7.0.4, 6.1.7)

🔹 Creating Your Own Gem

You can create and share your own gems with the Ruby community. Use bundle gem to generate a gem skeleton with all necessary files and structure.

# Create a new gem
bundle gem my_awesome_gem

# This creates:
# my_awesome_gem/
#   ├── lib/
#   │   └── my_awesome_gem.rb
#   ├── my_awesome_gem.gemspec
#   └── README.md

🔸 Simple Gem Example

# lib/my_awesome_gem.rb
module MyAwesomeGem
  def self.greet(name)
    "Hello, #{name}! Welcome to my gem!"
  end
end

# Usage
require 'my_awesome_gem'
puts MyAwesomeGem.greet("Ruby")

Output:

Hello, Ruby! Welcome to my gem!

🧠 Test Your Knowledge

What command installs a Ruby gem?