Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
108: Gemspec Configuration
Think of a .gemspec file like a detailed shipping manifest for a physical product. If you were shipping a DIY furniture kit, you wouldn't just throw a pile of wood in a box and hope for the best. You'd include a document that says: "This is the 'Nordic Coffee Table', version 2.0, created by Hans. To build this, you need a Phillips-head screwdriver and a hammer, and inside this box, you'll find four legs, one tabletop, and twelve screws."
In Ruby, the gemspec is that manifest. It tells RubyGems exactly what your library is, who is responsible for it, and—most importantly—what other pieces of software must be installed on the user's system for your code to actually run. Without it, your code is just a folder of scripts; with it, your code becomes a distributable package.
Defining Your Gem's Identity
When you first open a gemspec, you're looking at a Gem::Specification.new block. This is where you set the "metadata." I've seen a lot of beginners treat these fields as optional, but they aren't. If you're planning to publish this to RubyGems.org, the name and version are the primary keys the entire ecosystem uses to track your work.
Gem::Specification.new do |spec|
spec.name = "string_sorter"
spec.version = "0.1.0"
spec.authors = ["Your Name"]
spec.email = ["you@example.com"]
spec.summary = "A utility for alphabetically sorting complex strings."
spec.description = "This gem provides advanced sorting algorithms for strings that contain mixed numeric and alphabetic characters."
spec.homepage = "https://github.com/username/string_sorter"
spec.license = "MIT"
end
Notice the difference between summary and description. The summary is the "elevator pitch"—one short sentence. The description is the deeper dive. If you make them the same, it looks sloppy to anyone browsing your gem.
Managing Your Dependencies
This is where the "ingredients list" from our analogy comes in. You'll encounter two types of dependencies: runtime and development. This distinction is critical. If you mess this up, you'll either force your users to install a bunch of testing tools they don't need, or your gem will crash because it's missing a core library.
Runtime dependencies are things your code cannot function without. If your gem uses httparty to make API calls, httparty is a runtime dependency.
spec.add_dependency "httparty", "~> 0.21.0"
Development dependencies, on the other hand, are tools you need while you're writing the code, but the end-user doesn't need. I always put rspec or rubocop here. Your user doesn't need to run your tests to use your library, so don't bloat their system with your testing suite.
spec.add_development_dependency "rspec", "~> 3.12"
Telling Ruby What to Pack
One of the most frustrating "gotchas" I've run into is the spec.files array. If you don't tell the gemspec which files to include, RubyGems might just package everything in your folder—including your .gitignore, your README, and even your local environment secrets.
The standard professional approach is to use a Git command to dynamically list all tracked files. This ensures that only the code you've actually committed to version control gets shipped.
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|bin)/})
end
In the example above, I'm telling Ruby to grab everything Git knows about, but then I'm explicitly rejecting the test and spec folders. Why? Because your users don't need your test files sitting in their gems directory; they just need the lib folder.
📋 Practical Task
Configure the "ClimateQuery" Gemspec
You are building a gem called climate_query that fetches weather data from a remote API. You have the code written, but the climate_query.gemspec file is currently empty.
Complete the gemspec file with the following requirements:
- Identity: Set the name to
"climate_query"and the version to"1.0.0". - Authorship: Add yourself as the author and use
"dev@climatequery.io"as the email. - Runtime Dependency: The gem requires the
"rest-client"gem (any version) to make HTTP requests. - Development Dependency: You use
"rake"to automate your build tasks during development. - Files: Use the
git ls-filesmethod to populatespec.files, ensuring that thespec/directory is excluded from the final package.
Write the full Gem::Specification.new block that implements these configurations.
There are no comments for now.