A Ruby gem is a packaged library or tool that can be shared and reused in Ruby programs. It helps you add features without writing all code yourself.
Click to reveal answer
beginner
Which command creates a new gem skeleton in Ruby?
The command bundle gem gem_name creates a new folder with files and folders needed to start a gem.
Click to reveal answer
intermediate
What is the purpose of the gemspec file in a Ruby gem?
The gemspec file describes the gem’s name, version, author, summary, files, and dependencies. It tells RubyGems how to build and install the gem.
Click to reveal answer
beginner
How do you build a Ruby gem into a .gem file?
Run gem build gem_name.gemspec. This creates a .gem file you can install or share.
Click to reveal answer
beginner
How can you install your built gem locally?
Use gem install ./gem_name-version.gem to install the gem file on your computer.
Click to reveal answer
What does the command bundle gem my_gem do?
ACreates a new gem project skeleton named my_gem
BInstalls the gem named my_gem
CUpdates all gems including my_gem
DRemoves the gem named my_gem
✗ Incorrect
The command bundle gem my_gem sets up a new folder with files to start building a gem called my_gem.
Which file contains the gem’s metadata like version and author?
AGemfile
BREADME.md
Cgem_name.gemspec
Dlib/gem_name.rb
✗ Incorrect
The gemspec file holds important info about the gem such as its version, author, and dependencies.
How do you build the gem package from the gemspec file?
Agem install gem_name.gemspec
Bbundle install
Cruby gem_name.gemspec
Dgem build gem_name.gemspec
✗ Incorrect
The gem build command creates the .gem package from the gemspec.
What is the main folder where your gem’s Ruby code lives?
Abin/
Blib/
Cspec/
Ddoc/
✗ Incorrect
The lib/ folder contains the main Ruby files for your gem.
Which command installs a gem from a local .gem file?
Agem install ./gem_name-version.gem
Bgem install gem_name
Cbundle install gem_name
Druby install gem_name.gem
✗ Incorrect
Use gem install ./gem_name-version.gem to install a gem from a local file.
Explain the basic steps to create and share a Ruby gem.
Think about starting, describing, coding, packaging, and sharing.
You got /5 concepts.
What is the role of the gemspec file and why is it important?
It’s like the gem’s ID card and instruction sheet.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Ruby gem?
easy
A. To compile Ruby code into machine language
B. To package and share reusable Ruby code
C. To create graphical user interfaces
D. To manage Ruby versions on your system
Solution
Step 1: Understand what a gem is
A gem is a package that contains Ruby code that can be reused in different projects.
Step 2: Identify the main purpose
The main purpose is to share and reuse code easily across Ruby projects.
Final Answer:
To package and share reusable Ruby code -> Option B
Quick Check:
Gem = reusable Ruby package [OK]
Hint: Gems are for sharing code, not compiling or GUIs [OK]
Common Mistakes:
Thinking gems compile Ruby code
Confusing gems with Ruby version managers
Assuming gems create user interfaces
2. Which file is essential to define a Ruby gem's metadata and dependencies?
easy
A. README.md
B. Gemfile
C. Rakefile
D. gemspec
Solution
Step 1: Identify gem metadata file
The gemspec file (.gemspec) defines the gem's name, version, authors, and dependencies.
Step 2: Differentiate from other files
Gemfile manages project dependencies, Rakefile defines tasks, README.md is documentation.
Final Answer:
gemspec -> Option D
Quick Check:
Metadata and dependencies = gemspec [OK]
Hint: gemspec holds gem info; Gemfile is for app dependencies [OK]
Common Mistakes:
Confusing Gemfile with gemspec
Thinking README.md defines dependencies
Assuming Rakefile holds gem metadata
3. Given this gemspec snippet, what is the gem's version?
Gem::Specification.new do |spec|
spec.name = "my_gem"
spec.version = "0.2.5"
spec.summary = "A sample gem"
end
medium
A. "0.2.5"
B. "2.5"
C. "0.2.0"
D. "1.0.0"
Solution
Step 1: Locate the version assignment
The line spec.version = "0.2.5" sets the gem's version.
Step 2: Confirm the exact version string
The version is exactly "0.2.5" as given in the code.
Final Answer:
"0.2.5" -> Option A
Quick Check:
Version in gemspec = "0.2.5" [OK]
Hint: Look for spec.version assignment in gemspec [OK]
Common Mistakes:
Picking partial version like "0.2.0"
Confusing version with gem name
Assuming default version "1.0.0"
4. This gemspec file has an error. What is it?
Gem::Specification.new do |spec|
spec.name = "example_gem"
spec.version = 1.0.0
spec.summary = "Example gem"
end
medium
A. Version should be a string, not a number
B. Missing spec.authors field
C. spec.name cannot have underscores
D. spec.summary must be longer than 20 characters
Solution
Step 1: Check version format
The version is written as 1.0.0 without quotes, which Ruby treats as a numeric expression causing error.
Step 2: Correct version format
Version must be a string like "1.0.0" with quotes to be valid.
Final Answer:
Version should be a string, not a number -> Option A
Quick Check:
Version must be string in gemspec [OK]
Hint: Always quote version strings in gemspec [OK]
Common Mistakes:
Leaving version unquoted
Thinking underscores are invalid in gem names
Assuming summary length is strictly enforced
5. You want to create a gem that provides a method greet returning "Hello!". Which file structure and code snippet is correct?
Options:
A)
my_gem/
lib/
my_gem.rb
my_gem.gemspec
# lib/my_gem.rb
module MyGem
def self.greet
"Hello!"
end
end
B)
my_gem/
src/
greet.rb
gemspec.rb
# src/greet.rb
class Greet
def greet
puts "Hello!"
end
end
C)
my_gem/
lib/
greet.rb
my_gem.gemspec
# lib/greet.rb
module Greet
def greet
"Hello!"
end
end
D)
my_gem/
lib/
my_gem.rb
my_gem.gemspec
# lib/my_gem.rb
def greet
"Hello!"
end
hard
A. Wrong file name and missing self for method
B. Wrong folder and gemspec name; greet is instance method
C. Correct structure with module and class method greet
D. Method defined outside module, no namespacing
Solution
Step 1: Check folder and file structure
Ruby gems use lib/ folder and a gemspec named after the gem. Correct structure with module and class method greet uses lib/my_gem.rb and my_gem.gemspec, which is correct.
Step 2: Verify code correctness
Correct structure with module and class method greet defines a module MyGem with a class method self.greet returning "Hello!", which is the right way to provide a gem method.
Final Answer:
Correct structure with module and class method greet -> Option C