Home » Writing » Writing your own gem fusion

Writing your own gem fusion

Writing your own gem fusion That one file is in

From start to finish, learn how to package your Ruby code in a gem.

Introduction

Creating and publishing your own gem is simple thanks to the tools baked right into RubyGems. Let’s make a simple “hello world” gem, and feel free to play along at home! The code for the gem we’re going to make here is up on GitHub .

Your first gem

I started with just one Ruby file for my hola gem, and the gemspec. You’ll need a new name for yours (maybe hola_yourusername ) to publish it. Check the Patterns guide for basic recommendations to follow when naming a gem.

Code for your package is placed within the lib directory. The convention is to have one Ruby file with the same name as your gem, since that gets loaded when require ‘hola’ is run. That one file is in charge of setting up your gem’s code and API.

The code inside of lib/hola.rb is pretty bare bones. It just makes sure that you can see some output from the gem:

The gemspec defines what’s in the gem, who made it, and the version of the gem. It’s also your interface to RubyGems.org. All of the information you see on a gem page (like jekyll ’s) comes from the gemspec.

The description member can be much longer than you see in this example. If it matches /^== [A-Z]/ then the description will be run through RDoc’s markup formatter for display on the RubyGems web site. Be aware though that other consumers of the data might not understand this markup.

Look familiar? The gemspec is also Ruby, so you can wrap scripts to generate the file names and bump the version number. There are lots of fields the gemspec can contain. To see them all check out the full reference .

After you have created a gemspec, you can build a gem from it. Then you can install the generated gem locally to test it out.

Writing your own gem fusion so you can wrap scripts

Of course, the smoke test isn’t over yet: the final step is to require the gem and use it:

If you’re using an earlier Ruby than 1.9.2, you need to start the session with irb -rubygems or require the rubygems library after you launch irb.

Now you can share hola with the rest of the Ruby community. Publishing your gem out to RubyGems.org only takes one command, provided that you have an account on the site. To setup your computer with your RubyGems account:

If you’re having problems with curl, OpenSSL, or certificates, you might want to simply try entering the above URL in your browser’s address bar. Your browser will ask you to login to RubyGems.org. Enter your username and password. Your browser will now try to download the file api_key.yaml. Save it in

/.gem and call it ‘credentials’

Once this has been setup, you can push out the gem:

In just a short time (usually less than a minute), your gem will be available for installation by anyone. You can see it on the RubyGems.org site or grab it from any computer with RubyGems installed:

It’s really that easy to share code with Ruby and RubyGems.

Requiring more files

Having everything in one file doesn’t scale well. Let’s add some more code to this gem.

This file is getting pretty crowded. Let’s break out the Translator into a separate file. As mentioned before, the gem’s root file is in charge of loading code for the gem. The other files for a gem are usually placed in a directory of the same name of the gem inside of lib.

Writing your own gem fusion to the tools baked right

We can split this gem out like so:

The Translator is now in lib/hola. which can easily be picked up with a require statement from lib/hola.rb. The code for the Translator did not change much:

But now the hola.rb file has some code to load the Translator :

Gotcha: For newly created folder/file, do not forget to add one entry in hola.gemspec file, as shown-

without the above change, new folder would not be included into the installed gem.

Let’s try this out. First, fire up irb :

We need to use a strange command line flag here: -Ilib. Usually RubyGems includes the lib directory for you, so end users don’t need to worry about configuring their load paths. However, if you’re running the code outside of RubyGems, you have to configure things yourself. It’s possible to manipulate the $LOAD_PATH from within the code itself, but that’s considered an anti-pattern in most cases. There are many more anti-patterns (and good patterns!) for gems, explained in this guide .

If you’ve added more files to your gem, make sure to remember to add them to your gemspec’s files array before publishing a new gem! For this reason (among others), many developers automate this with Hoe. Jeweler. Rake. Bundler. or just a dynamic gemspec .

Adding more directories with more code from here is pretty much the same process. Split your Ruby files up when it makes sense! Making a sane order for your project will help you and your future maintainers from headaches down the line.

Adding an executable

In addition to providing libraries of Ruby code, gems can also expose one or many executable files to your shell’s PATH. Probably the best known example of this is rake. Another very useful one is prettify_json.rb. included with the JSON gem, which formats JSON in a readable manner (and is included with Ruby 1.9). Here’s an example:

Adding an executable to a gem is a simple process. You just need to place the file in your gem’s bin directory, and then add it to the list of executables in the gemspec. Let’s add one for the Hola gem. First create the file and make it executable:

The executable file itself just needs a shebang in order to figure out what program to run it with. Here’s what Hola’s executable looks like:

All it’s doing is loading up the gem, and passing the first command line argument as the language to say hello with. Here’s an example of running it:

Finally, to get Hola’s executable included when you push the gem, you’ll need to add it in the gemspec.

Push up that new gem, and you’ll have your own command line utility published! You can add more executables as well in the bin directory if you need to, there’s an executables array field on the gemspec.

Note that you should change the gem’s version when pushing up a new release. For more information on gem versioning, see the Patterns Guide

Writing tests

Testing your gem is extremely important. Not only does it help assure you that your code works, but it helps others know that your gem does its job. When evaluating a gem, Ruby developers tend to view a solid test suite (or lack thereof) as one of the main reasons for trusting that piece of code.

Gems support adding test files into the package itself so tests can be run when a gem is downloaded.

In short: TEST YOUR GEM! Please!

Test::Unit is Ruby’s built-in test framework. There are lots of tutorials for using it online. There are many other test frameworks available for Ruby as well. RSpec is a popular choice. At the end of the day, it doesn’t matter what you use, just TEST !

Let’s add some tests to Hola. This requires adding a few more files, namely a Rakefile and a brand new test directory:

The Rakefile gives you some simple automation for running tests:

Now you can run rake test or simply just rake to run tests. Woot! Here’s a basic test file for hola:

Finally, to run the tests:

It’s green! Well, depending on your shell colors. For more great examples, the best thing you can do is hunt around GitHub and read some code.

Documenting your code

By default most gems use RDoc to generate docs. There are plenty of great tutorials for learning how to mark up your code with RDoc. Here’s a simple example:

Another great option for documentation is YARD. since when you push a gem, RubyDoc.info generates YARDocs automatically from your gem. YARD is backwards compatible with RDoc, and it has a good introduction on what’s different and how to use it.

Wrapup

With this basic understanding of building your own RubyGem, we hope you’ll be on your way to making your own! The next few guides cover patterns in making a gem and the other capabilities of the RubyGems system.

Credits

This tutorial was adapted from Gem Sawyer, Modern Day Ruby Warrior. The code for this gem can be found on GitHub .


Share this:
custom writing low cost
Order custom writing

ads