📖 Scenario: You are working on a Ruby project that uses external libraries called gems. Managing these gems properly helps keep your project organized and avoids conflicts.
🎯 Goal: You will create a simple Ruby script that lists gems your project uses, sets a version requirement, and shows how to check gem versions. This helps you understand why managing gems is important.
📋 What You'll Learn
Create a hash called gems with gem names and their current versions
Create a variable called required_version to set a minimum version for a gem
Use a loop to check which gems meet the required version
Print the gems that meet the version requirement
💡 Why This Matters
🌍 Real World
Managing gem versions helps avoid bugs and conflicts in Ruby projects by ensuring all parts use compatible libraries.
💼 Career
Ruby developers must manage gems carefully to maintain stable and secure applications, making this skill important for professional coding.
Progress0 / 4 steps
1
Create the gems hash
Create a hash called gems with these exact entries: 'rails' => '7.0.4', 'nokogiri' => '1.13.3', 'puma' => '5.6.4'
Ruby
Hint
Use curly braces {} to create a hash with gem names as keys and versions as values.
2
Set the required version
Create a variable called required_version and set it to the string '7.0.0' to represent the minimum version required for gems
Ruby
Hint
Assign the string '7.0.0' to the variable required_version.
3
Check gems against the required version
Use a for loop with variables name and version to iterate over gems. Inside the loop, compare version with required_version using string comparison, and add gems that meet or exceed the required version to an array called valid_gems
Ruby
Hint
Use for name, version in gems to loop. Use valid_gems << name if version >= required_version to add valid gems.
4
Print the valid gems
Write a puts statement to print the string "Gems meeting version requirement:" and then print the valid_gems array
Ruby
Hint
Use puts to print the message and the array valid_gems.
Practice
(1/5)
1. Why is gem management important in Ruby projects?
easy
A. It makes Ruby code run faster automatically.
B. It keeps projects organized and ensures consistent gem versions.
C. It removes the need to write tests.
D. It converts Ruby code to another language.
Solution
Step 1: Understand gem management purpose
Gem management helps keep track of which gems and versions a project uses.
Step 2: Identify benefits of organization
Organizing gems prevents conflicts and ensures the project runs the same on all machines.
Final Answer:
It keeps projects organized and ensures consistent gem versions. -> Option B
Quick Check:
Gem management = organization and consistency [OK]
Hint: Gem management = organized gems and stable projects [OK]
Common Mistakes:
Thinking gem management speeds up code automatically
Believing gem management removes testing needs
Confusing gem management with code translation
2. Which of the following is the correct way to specify a gem in a Gemfile?
easy
A. install gem rails version 7.0
B. require 'rails', version: '7.0'
C. gem 'rails', '~> 7.0'
D. add gem rails 7.0
Solution
Step 1: Recall Gemfile syntax
In a Gemfile, gems are declared using the gem method with the name and version string.
Step 2: Match correct syntax
gem 'rails', '~> 7.0' uses gem 'name', 'version' format which is correct.
Final Answer:
gem 'rails', '~> 7.0' -> Option C
Quick Check:
Gemfile gem syntax = gem 'name', 'version' [OK]
Hint: Gemfile uses gem 'name', 'version' format [OK]
Common Mistakes:
Using commands like install or add inside Gemfile
Using require instead of gem in Gemfile
Incorrect version syntax
3. What will be the output of running bundle install in a Ruby project with a Gemfile?
medium
A. It installs all gems listed in the Gemfile and their dependencies.
B. It deletes all installed gems from the system.
C. It runs the Ruby program specified in the Gemfile.
D. It updates Ruby to the latest version.
Solution
Step 1: Understand the role of bundle install
The command bundle install reads the Gemfile and installs required gems.
Step 2: Identify correct behavior
It installs gems and their dependencies, not deleting or running code.
Final Answer:
It installs all gems listed in the Gemfile and their dependencies. -> Option A
Quick Check:
bundle install = install gems from Gemfile [OK]
Hint: bundle install installs gems from Gemfile [OK]
Common Mistakes:
Thinking bundle install deletes gems
Confusing bundle install with running Ruby code
Assuming it updates Ruby version
4. Given this Gemfile snippet:
gem 'nokogiri', '~> 1.12'
gem 'rails', '6.1.4'
What is wrong with this Gemfile if you want to update nokogiri to version 1.13.0?
medium
A. You need to specify the Ruby version to update nokogiri.
B. You must remove the rails gem to update nokogiri.
C. The Gemfile syntax is incorrect and causes errors.
D. The version constraint '~> 1.12' prevents updating to 1.13.0.
Solution
Step 1: Understand '~>' version constraint
The '~> 1.12' means allow updates up to but not including 1.13.0.
Step 2: Check if 1.13.0 is allowed
Since 1.13.0 is outside '~> 1.12', it cannot update to that version.
Final Answer:
The version constraint '~> 1.12' prevents updating to 1.13.0. -> Option D
Quick Check:
Version constraint '~> 1.12' blocks 1.13.0 update [OK]
Hint: ~> limits updates to next minor version, blocking bigger jumps [OK]
Common Mistakes:
Thinking rails gem affects nokogiri update
Believing Gemfile syntax is wrong here
Assuming Ruby version controls gem updates
5. You want to share your Ruby project with others and ensure they use the exact same gem versions. Which approach best achieves this?
hard
A. Use a Gemfile with version constraints and commit the Gemfile.lock file.
B. Only list gem names in the Gemfile without versions.
C. Install gems manually on each machine without a Gemfile.
D. Use system-wide gem installations without Bundler.
Solution
Step 1: Understand sharing gem versions
To share exact gem versions, you need both Gemfile and Gemfile.lock files.
Step 2: Identify role of Gemfile.lock
Gemfile.lock locks gem versions so others install the same ones.
Step 3: Evaluate options
Using a Gemfile with version constraints and committing the Gemfile.lock ensures consistency across machines.
Final Answer:
Use a Gemfile with version constraints and commit the Gemfile.lock file. -> Option A