Challenge - 5 Problems
RubyGems Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this RubyGems command?
Consider the following Ruby code that uses RubyGems to list installed gems. What will be printed?
Ruby
require 'rubygems' puts Gem::Specification.find_all.map(&:name).sort.first(3).join(', ')
Attempts:
2 left
💡 Hint
Gem::Specification.find_all returns all installed gems as objects. Mapping &:name extracts their names.
✗ Incorrect
The code lists all installed gems, sorts their names alphabetically, takes the first three, and joins them with commas.
🧠 Conceptual
intermediateWhich RubyGems command installs a gem and its dependencies?
You want to install a gem named 'nokogiri' along with all its dependencies. Which command should you use?
Attempts:
2 left
💡 Hint
Installing a gem downloads and installs it with dependencies.
✗ Incorrect
'gem install' downloads and installs the gem and its dependencies. 'fetch' only downloads the gem file, 'build' creates a gem from source, and 'uninstall' removes a gem.
❓ Predict Output
advancedWhat error does this RubyGems code raise?
What error will this Ruby code raise when run?
Ruby
require 'rubygems' Gem::Specification.find_by_name('nonexistent_gem')
Attempts:
2 left
💡 Hint
find_by_name raises an error if the gem is not found.
✗ Incorrect
If the gem name is not found, RubyGems raises Gem::MissingSpecError to indicate the gem is missing.
🚀 Application
advancedHow to programmatically list all versions of a gem installed?
You want to write Ruby code that lists all installed versions of the gem 'rake'. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
find_all_by_name returns all specs matching the gem name.
✗ Incorrect
find_all_by_name returns all installed specs for 'rake'. Mapping version and converting to string lists all installed versions.
🔧 Debug
expertWhy does this RubyGems code fail to install a gem?
This Ruby code tries to install the 'json' gem but fails with an error. What is the cause?
Ruby
require 'rubygems' require 'rubygems/installer' installer = Gem::Installer.new('json-2.6.1.gem') installer.install
Attempts:
2 left
💡 Hint
Check if the gem file exists at the given path.
✗ Incorrect
Gem::Installer expects a valid gem file path. If the file is missing or path is wrong, installation fails.
