What I Learned Building My First Ruby Gem (Peeking Behind the Rails Curtain)

When you’re working on Rails apps every day, gems feel like magic. You install them, maybe tweak a config file, and boom — they work. But when I built my first Ruby gem, audit_log_vk (In case you are wondering, I'm Vk, that's why that name), I realized just how much was happening behind the curtain. As a developer, it was eye-opening to see what really goes into making a gem that feels seamless to the end user. Here are some of the most surprising things I discovered — and why building a gem, even a small one, is totally worth it. 1. A Gem Is Just a Folder — But with Specific Files That Unlock Everything The first surprise was that a gem is really just a Ruby project with the right folder structure. But the magic happens because of a few specific files: audit_log_vk.gemspec This file defines the gem itself — name, summary, author, license, and (importantly) what files to include when it’s packaged. It’s like a package.json in the Node.js world, but with more manual responsibility. # Set the gem version by referencing the constant inside lib/audit_log_vk/version.rb spec.version = AuditLog::VERSION # Define which files to include in the gem package spec.files = Dir.chdir(__dir__) do # Use Git to list all version-controlled files, separated by null characters `git ls-files -z`.split("\x0").reject do |f| # Exclude this gemspec file itself and unwanted paths (tests, CI configs, etc.) (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile]) end end

Apr 22, 2025 - 18:26
 0
What I Learned Building My First Ruby Gem (Peeking Behind the Rails Curtain)

When you’re working on Rails apps every day, gems feel like magic. You install them, maybe tweak a config file, and boom — they work. But when I built my first Ruby gem, audit_log_vk (In case you are wondering, I'm Vk, that's why that name), I realized just how much was happening behind the curtain. As a developer, it was eye-opening to see what really goes into making a gem that feels seamless to the end user.

Here are some of the most surprising things I discovered — and why building a gem, even a small one, is totally worth it.

1. A Gem Is Just a Folder — But with Specific Files That Unlock Everything

The first surprise was that a gem is really just a Ruby project with the right folder structure. But the magic happens because of a few specific files:

audit_log_vk.gemspec

This file defines the gem itself — name, summary, author, license, and (importantly) what files to include when it’s packaged. It’s like a package.json in the Node.js world, but with more manual responsibility.

# Set the gem version by referencing the constant inside lib/audit_log_vk/version.rb
spec.version = AuditLog::VERSION

# Define which files to include in the gem package
spec.files = Dir.chdir(__dir__) do
  # Use Git to list all version-controlled files, separated by null characters
  `git ls-files -z`.split("\x0").reject do |f|
    # Exclude this gemspec file itself and unwanted paths (tests, CI configs, etc.)
    (File.expand_path(f) == __FILE__) ||
      f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
  end
end