Bird
Raised Fist0
Rubyprogramming~20 mins

RubyGems repository - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
RubyGems Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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(', ')
AA comma-separated list of the first three gem names alphabetically installed on the system
BA list of all gem names installed, unsorted
CAn error because Gem::Specification.find_all is not a valid method
DAn empty string because no gems are found
Attempts:
2 left
💡 Hint
Gem::Specification.find_all returns all installed gems as objects. Mapping &:name extracts their names.
🧠 Conceptual
intermediate
1:30remaining
Which 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?
Agem uninstall nokogiri
Bgem install nokogiri
Cgem build nokogiri
Dgem fetch nokogiri
Attempts:
2 left
💡 Hint
Installing a gem downloads and installs it with dependencies.
❓ Predict Output
advanced
2:00remaining
What 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')
AGem::MissingSpecError
BNoMethodError
CArgumentError
DRuntimeError
Attempts:
2 left
💡 Hint
find_by_name raises an error if the gem is not found.
🚀 Application
advanced
2:30remaining
How 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?
AGem::Specification.list_versions('rake')
BGem::Specification.find_by_name('rake').version.to_s
CGem::Specification.latest_version('rake')
DGem::Specification.find_all_by_name('rake').map(&:version).map(&:to_s)
Attempts:
2 left
💡 Hint
find_all_by_name returns all specs matching the gem name.
🔧 Debug
expert
3:00remaining
Why 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
AGem::Installer requires a block to run
BThe gem 'json' is already installed, so install fails
CThe gem file path 'json-2.6.1.gem' is missing or incorrect
DGem::Installer cannot install gems programmatically
Attempts:
2 left
💡 Hint
Check if the gem file exists at the given path.

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

  1. Step 1: Understand what RubyGems stores

    RubyGems repository is known for storing Ruby libraries called gems.
  2. Step 2: Identify the main function

    Its main function is to share these gems so developers can reuse code easily.
  3. Final Answer:

    To store and share Ruby libraries called gems -> Option A
  4. 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

  1. Step 1: Recall the correct gem installation syntax

    The standard command to install a gem is gem install gem_name.
  2. Step 2: Match the command with the gem name

    For the gem named rails, the command is gem install rails.
  3. Final Answer:

    gem install rails -> Option A
  4. 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?
require 'json'
puts JSON.generate({name: 'Ruby', type: 'language'})
medium
A. SyntaxError
B. {name: Ruby, type: language}
C. {"name":"Ruby","type":"language"}
D. nil

Solution

  1. Step 1: Understand the require statement

    The code requires the 'json' gem, which provides JSON methods.
  2. Step 2: Analyze JSON.generate output

    JSON.generate converts the Ruby hash to a JSON string with keys and values in quotes.
  3. Final Answer:

    {"name":"Ruby","type":"language"} -> Option C
  4. Quick Check:

    JSON.generate outputs JSON string [OK]
Hint: JSON.generate outputs string with quotes [OK]
Common Mistakes:
  • Expecting Ruby hash syntax as output
  • Confusing JSON.generate with JSON.parse
  • Ignoring quotes around keys and values
4. Identify the error in this Ruby code snippet that tries to use the 'httparty' gem:
require 'httparty'
response = HTTParty.get('https://api.example.com/data')
puts response.body
medium
A. Missing parentheses in the require statement
B. The method 'get' does not exist in HTTParty
C. Cannot call 'body' on response
D. The gem 'httparty' is not installed

Solution

  1. Step 1: Check if 'httparty' gem is installed

    If the gem is not installed, require will fail or HTTParty will be undefined.
  2. Step 2: Verify method and usage

    HTTParty.get is a valid method, and response.body is valid if response is returned.
  3. Final Answer:

    The gem 'httparty' is not installed -> Option D
  4. Quick Check:

    Missing gem causes require failure [OK]
Hint: Install gem before requiring it [OK]
Common Mistakes:
  • Assuming require adds gem automatically
  • Thinking 'get' method is invalid
  • Ignoring gem installation step
5. You want to create a Ruby script that uses the 'colorize' gem to print text in red. Which sequence of steps is correct?
hard
A. Run require 'colorize', then gem install colorize, then use puts 'text'.colorize(:red)
B. Run gem install colorize, then require 'colorize', then use puts 'text'.colorize(:red)
C. Use puts 'text'.colorize(:red) directly without installing or requiring
D. Run gem add colorize, then require 'colorize', then use puts 'text'.colorize(:red)

Solution

  1. Step 1: Install the gem first

    You must install the gem using gem install colorize before using it.
  2. Step 2: Require the gem in your script

    Use require 'colorize' to load the gem's code.
  3. Step 3: Use the gem's method to color text

    Call puts 'text'.colorize(:red) to print red text.
  4. Final Answer:

    Run gem install, then require, then use colorize method -> Option B
  5. Quick Check:

    Install -> require -> use gem [OK]
Hint: Install gem before require and usage [OK]
Common Mistakes:
  • Requiring gem before installing
  • Using 'gem add' instead of 'gem install'
  • Trying to use gem methods without require
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard