Skip to main content

How to Create a Custom Gem

· 3 min read
ひかり
Main bloger

Create a gem (using bundle gem)

To create a template for a gem named gem_name, execute the following:

bundle gem gem_name

Options for bundle gem

bundle gem has options available.

For details or other options, please refer to Bundler: bundle gem.

OptionDescription
-–exe, -b, --binInclude binary executables in the gem.
-–extInclude C extensions in the gem.
-–mitMake the project an MIT license.
-–ciSet up CI services.
-–linterSpecify linters and formatters.

Example: To create a gem named myapp with the ruboCop linter, an MIT license, and including commands with a C language extension:

bundle gem myapp --exe --ext --mit --ci=github --linter=ruboCop

Project Structure

The project structure generated by the above command will look like this:

myapp
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│ ├── console
│ └── setup
├── exe
│ └── myapp
├── ext
│ └── myapp
│ ├── extcofn.rb
│ ├── myapp.c
│ └── myapp.h
├── lib
│ ├── myapp
│ │ └── version.rb
│ └── myapp.rb
├── myapp.gemspec
└── sig
└── myapp.rb

Each directory is structured as follows:

  • lib/: Library code (called with require)
  • ext/: C language source code (compiled during installation and called with require)
  • exe/: Contains commands

myapp.gemspec is the most important file, defining the gem's configuration. You'll primarily edit files within these three directories (or two if you're not using C) and the gemspec file (editing Rakefile and README.md as needed).

Do not edit Gemfile.

Editing gemspec

You need to edit myapp.gemspec to create the gem.

Commenting out lines with TODOs and the homepage line, and setting the spec.summary will be the minimum required to make it work.

The following items are mandatory:

  • spec.name
  • spec.version
  • spec.authors
  • spec.summary

Run the bundle install command, and it should be fine if there are no errors.

Adding Dependency Packages

You can add dependencies to the gemspec like this:

spec.add_dependency "example-gem", "~> 1.0"

You can add development-only dependencies like this:

spec.add_development_dependency "example-gem", "~> 1.0"

For more details, refer to class Gem::Specification (Ruby 3.1) Reference Manual and libraryrubygems (Ruby 3.1 Reference Manual).

bundle install

Running the bundle install command will install the gems specified in the Gemfile and gemspec.

Version Changes

The version is defined in lib/myapp/version.rb.

Compilation

Run the following command to compile the C extensions:

rake compile

After compilation, executing commands included in the gem will run without errors.

bundle exec myapp

C Extensions

You can extend Ruby with C.

For example, modify /etc/myapp/myapp.c as follows:

void
Init_myapp(void)
{
rb_mMyapp = rb_define_module("Myapp");
rb_p(rb_str_new2("Hello, world!"));
}

After compiling with rake compile and running bundle exec myapp, it will display "Hello, world!".

This example shows a program that calls the Ruby C API from C, equivalent to:

p "Hello, world!"

Not only can you use the Ruby C API, but you can also access shared libraries installed on the computer, allowing you to create wrappers.

Additionally, C extensions can be used to speed up calculations.

Creating Libraries

Edit files in lib/myapp.rb, etc.

If you create a new file, you need to git add it.

Install Gem (rake install)

The gem within the project will be installed.

rake install

Publish Gem (rake release)

To publish the gem, you need to create a RubyGems account. Each time you release, you need to change the version.

rake release

Generate Gem Package (rake build)

A pkg/myapp-x.x.x.gem file will be created from the project.

rake build