This chapter focuses on understanding Ruby Gems, installing and using them, and creating your own simple Gem. Master these core concepts to streamline your Ruby programming workflow.
Topic 1: What are Gems?
What is a Ruby gem? a) A Ruby framework for building applications b) A packaged library or tool to extend Ruby functionality c) A method of organizing Ruby code d) A type of object in Ruby
Which command is used to list installed gems in Ruby? a) gem list b) gem show c) gem info d) gem display
Which file is typically used to list the dependencies of a Ruby gem? a) Gemfile b) gemspec c) gemspecs d) Gemfile.lock
What does the gem command allow you to do? a) Manage Ruby installations b) Install, update, and uninstall gems c) Compile Ruby code d) Edit Ruby scripts
What is the primary purpose of gems in Ruby? a) To help organize code into smaller modules b) To add third-party functionality to Ruby applications c) To manage Ruby versions d) To optimize Ruby performance
What is a .gemspec file used for? a) To specify gem dependencies b) To configure environment variables c) To define gem metadata for a gem d) To lock gem versions
Which command would you use to create a new gem? a) gem new b) gem create c) bundle gem d) ruby gem create
How can you check the version of a gem installed in your environment? a) gem -v b) gem version c) gem list [gem_name] d) gem check
How are gems typically installed in a Ruby project? a) By adding the gem’s name to the Gemfile b) Using the install command in the terminal c) Using gem install [gem_name] d) All of the above
Which of the following is an example of a Ruby gem that provides testing capabilities? a) rails b) rspec c) rails d) sinatra
Topic 2: Installing and Using Gems
Which command installs a gem from the RubyGems repository? a) gem install [gem_name] b) gem add [gem_name] c) gem get [gem_name] d) gem fetch [gem_name]
How do you install gems specified in a Gemfile? a) gem install --bundler b) bundle install c) gem install gemfile d) gem install --file Gemfile
What is the purpose of the bundler gem? a) It installs Ruby versions b) It manages gem dependencies for projects c) It helps create new gems d) It optimizes gem installations
What does the gem uninstall [gem_name] command do? a) Removes the gem and all of its dependencies b) Uninstalls the gem from the project c) Uninstalls a gem globally d) Both b and c
How do you update a gem to its latest version? a) gem update [gem_name] b) gem install --latest [gem_name] c) gem upgrade [gem_name] d) gem refresh [gem_name]
How can you install a gem from a local file? a) gem install file:[path_to_gem] b) gem add file:[path_to_gem] c) gem local install [gem_name] d) gem install --local
How do you install a specific version of a gem? a) gem install [gem_name] --version [version_number] b) gem install [gem_name]:[version_number] c) gem install [gem_name]@version [version_number] d) gem version install [gem_name]
Which of the following is a valid way to require a gem in your Ruby script? a) require 'gem_name' b) gem 'gem_name' c) include 'gem_name' d) use 'gem_name'
What does the gem list command do? a) Shows all available gems in the system b) Lists installed gems and their versions c) Displays gem dependencies for the project d) Lists gems in the Gemfile
Which command checks if a gem is up-to-date? a) gem status b) gem check c) gem outdated d) gem update --check
Topic 3: Creating a Simple Gem
How do you create a basic structure for a new gem? a) bundle gem [gem_name] b) gem create [gem_name] c) gem new [gem_name] d) rails gem create [gem_name]
What is the default directory structure generated by the bundle gem command? a) lib/, tests/, README.md b) lib/, spec/, Gemfile c) src/, docs/, config/ d) src/, tests/, Gemfile
What is the primary purpose of the .gemspec file in a gem? a) To specify gem dependencies and metadata b) To store Ruby code for the gem c) To test the gem d) To store the gem’s documentation
Which directory contains the main functionality of the gem? a) lib/ b) bin/ c) src/ d) docs/
How do you test the gem in your local environment before publishing it? a) gem push b) gem build c) rake gem:test d) gem install .
What is the purpose of the rake command in gem development? a) To compile Ruby code b) To install gems locally c) To automate tasks like testing and building the gem d) To list available gems
How do you specify a gem’s version in the .gemspec file? a) spec.version = '1.0.0' b) version = '1.0.0' c) gem.version = '1.0.0' d) spec.version('1.0.0')
How do you install a local gem during development? a) gem install . b) gem install --path . c) bundle install . d) gem install [gem_name]
How do you push a gem to RubyGems after it’s created? a) gem push [gem_name].gem b) gem publish [gem_name].gem c) bundle push [gem_name].gem d) gem upload [gem_name].gem
What is the typical extension of a Ruby gem file? a) .gem b) .rb c) .zip d) .tar
Answers Table
QNo
Answer
1
b) A packaged library or tool to extend Ruby functionality
2
a) gem list
3
a) Gemfile
4
b) Install, update, and uninstall gems
5
b) To add third-party functionality to Ruby applications
6
c) To define gem metadata for a gem
7
c) bundle gem
8
c) gem list [gem_name]
9
d) All of the above
10
b) rspec
11
a) gem install [gem_name]
12
b) bundle install
13
b) It manages gem dependencies for projects
14
d) Both b and c
15
a) gem update [gem_name]
16
a) gem install file:[path_to_gem]
17
a) gem install [gem_name] --version [version_number]
18
a) require 'gem_name'
19
b) Lists installed gems and their versions
20
c) gem outdated
21
a) bundle gem [gem_name]
22
b) lib/, spec/, Gemfile
23
a) To specify gem dependencies and metadata
24
a) lib/
25
d) gem install .
26
c) To automate tasks like testing and building the gem