📖 Scenario: You are managing a small collection of Ruby gems with their versions and descriptions. You want to organize this data and display gems that meet a certain version requirement.
🎯 Goal: Build a Ruby program that stores gem information in a hash, sets a minimum version requirement, filters gems that meet this version, and prints their names and versions.
📋 What You'll Learn
Create a hash called gems with gem names as keys and their version and description as values
Create a variable called min_version to hold the minimum version string
Use a select method to filter gems with version greater than or equal to min_version
Print the filtered gems' names and versions
💡 Why This Matters
🌍 Real World
Managing Ruby gems and their versions is common when developing Ruby applications to ensure compatibility and update control.
💼 Career
Understanding how to organize and filter gem data helps in roles like Ruby developer, DevOps engineer, or software maintainer.
Progress0 / 4 steps
1
Create the gems hash
Create a hash called gems with these exact entries: 'rails' => { version: '7.0.4', description: 'Web application framework' }, 'nokogiri' => { version: '1.13.3', description: 'HTML, XML parser' }, and 'devise' => { version: '4.8.1', description: 'Authentication solution' }.
Ruby
Hint
Use a Ruby hash with string keys and nested hashes for version and description.
2
Set the minimum version
Create a variable called min_version and set it to the string '5.0.0'.
Ruby
Hint
Just assign the string '5.0.0' to min_version.
3
Filter gems by version
Create a variable called filtered_gems that selects gems from gems where the gem's version is greater than or equal to min_version. Use string comparison on the version strings.
Ruby
Hint
Use select with a block comparing info[:version] and min_version.
4
Print filtered gems
Use a for loop with variables name and info to iterate over filtered_gems. Inside the loop, print the gem name and version in the format: "#{name}: #{info[:version]}".
Ruby
Hint
Use a for loop and puts with string interpolation.
Practice
(1/5)
1. What is the primary purpose of the RubyGems repository?
easy
A. To store and share Ruby libraries called gems
B. To compile Ruby code into machine language
C. To run Ruby programs faster
D. To create graphical user interfaces in Ruby
Solution
Step 1: Understand what RubyGems stores
RubyGems repository is known for storing Ruby libraries called gems.
Step 2: Identify the main function
Its main function is to share these gems so developers can reuse code easily.
Final Answer:
To store and share Ruby libraries called gems -> Option A
Quick Check:
RubyGems = Ruby libraries storage [OK]
Hint: RubyGems = place for Ruby code packages [OK]
Common Mistakes:
Thinking RubyGems compiles code
Confusing RubyGems with Ruby interpreter
Assuming RubyGems runs programs
2. Which command correctly installs a gem named rails from the RubyGems repository?
easy
A. gem install rails
B. install gem rails
C. ruby gem add rails
D. gem add rails
Solution
Step 1: Recall the correct gem installation syntax
The standard command to install a gem is gem install gem_name.
Step 2: Match the command with the gem name
For the gem named rails, the command is gem install rails.
Final Answer:
gem install rails -> Option A
Quick Check:
Install gem = gem install [OK]
Hint: Use 'gem install gemname' to add gems [OK]
Common Mistakes:
Swapping 'install' and 'gem' order
Using 'gem add' instead of 'gem install'
Adding extra words like 'ruby' before command
3. What will be the output of this Ruby code if the gem 'json' is installed?