Challenge - 5 Problems
Gem Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output when requiring gems without version control?
Consider a Ruby project that requires the 'json' gem. If the system has multiple versions of 'json' installed, what will be the output of the following code when run without specifying a gem version?
Ruby
require 'json' puts JSON.generate({hello: 'world'})
Attempts:
2 left
💡 Hint
Think about what happens when Ruby loads the default gem version.
✗ Incorrect
Without specifying a gem version, Ruby loads the default installed version of the gem. The code outputs the JSON string representation of the hash.
🧠 Conceptual
intermediate1:30remaining
Why lock gem versions in a Gemfile?
Why is it important to lock gem versions in a Gemfile when managing Ruby projects?
Attempts:
2 left
💡 Hint
Think about what happens if different developers use different gem versions.
✗ Incorrect
Locking gem versions ensures everyone uses the same gem versions, preventing bugs caused by version differences.
🔧 Debug
advanced2:30remaining
Identify the error caused by missing gem version specification
Given the following Gemfile and Ruby code, what error will occur when running the code if the gem version is not specified and multiple versions are installed?
Gemfile:
gem 'nokogiri'
Ruby code:
require 'nokogiri'
puts Nokogiri::HTML('
Hello
').textRuby
require 'nokogiri' puts Nokogiri::HTML('<h1>Hello</h1>').text
Attempts:
2 left
💡 Hint
Consider what happens if the gem loads correctly.
✗ Incorrect
If nokogiri is installed, even without version locking, the code outputs 'Hello' as expected.
📝 Syntax
advanced1:30remaining
Which Gemfile syntax correctly specifies a gem version?
Select the correct syntax to specify version 2.1.0 of the 'rails' gem in a Gemfile.
Attempts:
2 left
💡 Hint
Look for the correct Ruby method argument syntax.
✗ Incorrect
The correct syntax uses a comma to separate the gem name and version string.
🚀 Application
expert3:00remaining
How does Bundler ensure consistent gem versions across environments?
Bundler uses a special file to lock gem versions. What is the name of this file and how does Bundler use it to maintain consistency?
Attempts:
2 left
💡 Hint
Think about the file Bundler creates after installing gems.
✗ Incorrect
Bundler creates Gemfile.lock to record exact gem versions and uses it to install the same versions on other machines.