A step-by-step guide to packaging, distributing, and sharing your Ruby code with the world.

๐ Table of Contents
- โจ Why create your own gem?
- โณ When should you create a gem?
- ๐ What is a Ruby gem?
- ๐ More about RubyGems.org
- ๐ Best practices for gem development
- ๐ ๏ธ How to build your own gem
- ๐ Using and development
- ๐ Publish your gem
- ๐๏ธ Gem engine creation in Rails
- ๐ง How to override a gem engine
- Overriding models and controllers
- Overriding views
- Other ways to override
- ๐ฉบ Troubleshooting common issues
- ๐ Useful resources
- ๐ฌ Demo
Why create your own gem?
Creating your own gem not only helps you reuse code across multiple projects but also allows you to contribute to the Ruby community. By packaging your code as a gem, you make it easier for others to install, update, and collaborate on your work.
When should you create a gem?
- When you have reusable code or functionality that could benefit multiple projects.
- When you want to share your solution with the open-source community.
- When you need to modularize a large application for better maintainability.
What is a Ruby gem?
Ruby gems are open-source libraries or applications that add extra utility to Ruby programs. Each gem includes:
- Source code (with tests)
- Documentation and metadata (name, version, platform)
- A
.gemspec
file with gem-related metadata
Gems can be installed and published via RubyGems.org.
More about RubyGems.org
RubyGems.org is the central repository for publishing and sharing Ruby gems. It provides tools for gem management, versioning, and dependency resolution, making it easy for developers to distribute and use Ruby libraries.
Best practices for gem development
- Write clear documentation: Include a README and usage examples.
- Follow semantic versioning: Use version numbers to communicate changes.
- Add tests: Ensure your gem works as expected and is easy to maintain.
- Keep dependencies minimal: Avoid unnecessary dependencies to reduce conflicts.
- Use a consistent code style: Follow Ruby community conventions for readability.
How to build your own gem
Step 1: Generate gem structure
gem install bundler
bundle gem tony_demo_gem

Step 2: Edit the .gemspec
file

Step 3: Build and install your gem locally
gem build tony_demo_gem.gemspec
gem install ./tony_demo_gem-0.1.0.gem

Using and development
You can install gems from RubyGems.org, GitHub, or a local path.
From GitHub (in your Gemfile
):
gem "tony_demo_gem", git: "https://github.com/tonynguyen-mfv/tony_demo_gem.git"
From a local path:
gem "tony_demo_gem", path: "/Projects/tony_demo_gem"
From RubyGems.org:
gem "tony_demo_gem"
After editing your Gemfile
, run:
bundle install
โ ๏ธ Warning: When installing gems directly from GitHub, you bypass RubyGems.org’s infrastructure. Only do this if you trust the source.
Publish your gem
After building your gem, publish it to RubyGems.org:
gem signin
gem push tony_demo_gem-0.1.0.gem



Gem engine creation in Rails
A Rails engine is a gem that behaves like a miniature Rails application.
Step 1: Generate a new engine
rails plugin new tony_demo_plugin --mountable --full

Step 2: Generate models, controllers, and views
bin/rails generate scaffold User name:string email:string

Step 3: Import your gem into a Rails app
gem "tony_demo_plugin", path: "path/to/tony_demo_plugin"
Step 4: Mount your gem’s route
mount TonyDemoPlugin::Engine => "/tony_demo_plugin"
Step 5: Run migrations
rails tony_demo_plugin:install:migrations
rails db:migrate
Visit: http://localhost:3000/tony_demo_plugin/users/

How to override a gem engine
Overriding models and controllers
You can reopen engine models and controllers in the parent application to extend or decorate them.
๐ก Tip: Organize overrides in a dedicated directory (e.g.,
app/overrides
), ignored by the autoloader and preloaded in ato_prepare
callback.

Override an engine model:


Override an engine controller:

Overriding views
Override a view by creating a new file at app/views/*
in your application.

Other ways to override
See the Rails Engines Guide for more advanced techniques.
Troubleshooting common issues
- Gem not found: Make sure your gemspec is correct and the gem is published to RubyGems.org.
- Dependency conflicts: Check your gem’s dependencies and use version constraints.
- Installation errors: Ensure you have the correct Ruby version and required system libraries.
Useful resources
Demo
Start building your own gem today! Whether you’re solving a problem for yourself or sharing your work with the world, creating a gem is a rewarding way to grow as a Ruby developer and contribute to the community.
Inspired by the original post on Money Forward Vietnam Engineers’ Blog