📖 Scenario: You are building a simple Ruby project that needs external libraries (gems) to work. To manage these libraries easily, you will use Bundler, a tool that helps Ruby projects keep track of and install the right versions of gems.
🎯 Goal: Create a Ruby project that uses Bundler to manage dependencies. You will write a Gemfile listing the gems your project needs, configure Bundler, install the gems, and then write Ruby code that uses one of the installed gems.
📋 What You'll Learn
Create a Gemfile with specific gems and versions
Use Bundler to install the gems
Write Ruby code that requires and uses a gem from the Gemfile
Print output showing the gem is working
💡 Why This Matters
🌍 Real World
Bundler is used in almost every Ruby project to manage external libraries safely and easily. It helps avoid version conflicts and makes sharing projects simple.
💼 Career
Knowing how to use Bundler is essential for Ruby developers, especially when working on web apps, scripts, or any project that depends on third-party gems.
Progress0 / 4 steps
1
Create a Gemfile with dependencies
Create a file named Gemfile and write these exact lines inside it: source 'https://rubygems.org' gem 'colorize', '~> 0.8.1'
Ruby
Hint
The Gemfile tells Bundler which gems to install. Use the exact gem name and version.
2
Install gems using Bundler
Run the command bundle install in your terminal to install the gems listed in the Gemfile. Then create a Ruby file named app.rb and write require 'bundler/setup' at the top.
Ruby
Hint
Running bundle install downloads the gems. The require 'bundler/setup' line loads Bundler in your Ruby script.
3
Require and use the gem in Ruby code
In app.rb, add require 'colorize' below require 'bundler/setup'. Then write puts 'Hello, Bundler!'.colorize(:green) to print green text.
Ruby
Hint
The colorize gem lets you print colored text. Use puts with .colorize(:green) to show green text.
4
Run the Ruby script and see the output
Run ruby app.rb in your terminal. The output should be the text Hello, Bundler! shown in green color.
Ruby
Hint
When you run the script, the text should appear in green if your terminal supports colors.
Practice
(1/5)
1. What is the main purpose of Bundler in a Ruby project?
easy
A. To run Ruby code faster by caching results
B. To compile Ruby code into machine language
C. To create user interfaces for Ruby applications
D. To manage and install gem dependencies with specific versions
Solution
Step 1: Understand Bundler's role
Bundler is designed to handle gem dependencies and ensure the correct versions are used in a project.
Step 2: Compare options
Options B, C, and D describe unrelated tasks like compiling, UI creation, or caching, which Bundler does not do.
Final Answer:
To manage and install gem dependencies with specific versions -> Option D
Quick Check:
Bundler = dependency manager [OK]
Hint: Bundler manages gems and versions for your Ruby project [OK]
Common Mistakes:
Confusing Bundler with Ruby interpreter
Thinking Bundler compiles code
Assuming Bundler creates user interfaces
2. Which of the following is the correct command to install gems listed in a Gemfile using Bundler?
easy
A. bundle install
B. gem install
C. bundle update
D. ruby install
Solution
Step 1: Identify the command to install gems from Gemfile
The command bundle install reads the Gemfile and installs the specified gems.
Step 2: Differentiate from other commands
gem install installs a single gem, bundle update updates gems, and ruby install is invalid.
Final Answer:
bundle install -> Option A
Quick Check:
Install gems with bundle install [OK]
Hint: Use 'bundle install' to install all gems from Gemfile [OK]