📖 Scenario: You are starting a new Ruby project and need to manage the libraries (called gems) your project will use. Ruby uses a file named Gemfile to list these dependencies so they can be installed easily.
🎯 Goal: Create a Gemfile that lists specific gems your project needs, then add a Ruby version requirement, and finally print the list of gems to confirm your setup.
📋 What You'll Learn
Create a Gemfile with exact gem entries
Add a Ruby version requirement in the Gemfile
Use Ruby code to read and print the gem names from the Gemfile
💡 Why This Matters
🌍 Real World
Managing project dependencies with a Gemfile is essential for Ruby projects to ensure consistent environments and easy setup.
💼 Career
Ruby developers and DevOps engineers use Gemfiles daily to manage libraries and maintain project stability.
Progress0 / 4 steps
1
Create a Gemfile with gems
Create a file named Gemfile and add these exact gem entries: gem 'rails', '7.0.4', gem 'puma', '5.6.4', and gem 'nokogiri', '1.14.4'.
Ruby
Hint
Start your Gemfile with source 'https://rubygems.org' then add each gem line exactly as shown.
2
Add Ruby version requirement
Add the exact line ruby '3.2.2' at the top of the Gemfile to specify the Ruby version your project uses.
Ruby
Hint
The Ruby version line should be the very first line in the Gemfile.
3
Read gem names from Gemfile
Write Ruby code that reads the Gemfile and creates a list called gem_names containing only the gem names as strings (e.g., 'rails', 'puma', 'nokogiri'). Use File.readlines('Gemfile') and string methods to extract the names.
Ruby
Hint
Use map to transform lines and compact to remove nil values.
4
Print the list of gem names
Write a puts statement to print the gem_names list so the output shows: ["rails", "puma", "nokogiri"]
Ruby
Hint
Use puts gem_names.inspect to print the array as shown.
Practice
(1/5)
1. What is the main purpose of a Gemfile in a Ruby project?
easy
A. To configure the web server
B. To write Ruby program logic
C. To store database credentials
D. To list all the gems the project depends on
Solution
Step 1: Understand the role of Gemfile
A Gemfile is used to specify which gems (libraries) a Ruby project needs to work properly.
Step 2: Compare options with Gemfile purpose
Only To list all the gems the project depends on correctly states that Gemfile lists project gem dependencies.
Final Answer:
To list all the gems the project depends on -> Option D
Quick Check:
Gemfile purpose = list gems [OK]
Hint: Gemfile lists gems needed for the project [OK]
Common Mistakes:
Confusing Gemfile with Ruby code files
Thinking Gemfile stores secrets or configs
Assuming Gemfile runs the program
2. Which line is the correct syntax to add the gem 'rails' version 7.0.0 in a Gemfile?
easy
A. gem 'rails', '7.0.0'
B. gem rails, '7.0.0'
C. gem 'rails' version '7.0.0'
D. gem 'rails' => '7.0.0'
Solution
Step 1: Recall Gemfile gem syntax
The correct syntax uses the keyword gem, gem name in quotes, then version as a string separated by a comma.
Step 2: Check each option
gem 'rails', '7.0.0' matches the correct syntax: gem 'rails', '7.0.0'. Others have syntax errors or wrong keywords.
Final Answer:
gem 'rails', '7.0.0' -> Option A
Quick Check:
Gemfile gem syntax = gem 'name', 'version' [OK]
Hint: Use gem 'name', 'version' format in Gemfile [OK]