While working on one of our projects that uses the ActiveAdmin gem, I started wondering, what is a gem exactly? It works like a library, gives your application some ready-to-use functionality, but how does this happen?
Every RoR developer will have to modify a gem at least once in his or her career, but not everybody has actually created a gem. This is what got me going. I had to know exactly HOW you create a gem.
As it turns, out it’s a little bit like developing in Rails itself. You start with a powerful set of tools to help you right away.
To get a gem started you need to run:
bundle gem your_gem_name
This will create some basic files and directories that you need to start developing a gem. For example:
Don’t think for a second that the name of a gem is some random string though. Keeping such a large and open community as is RoR in order needs defining guidelines for pretty much everything. This is no exception:
Let me give you some examples to make it clear:
Ok, let’s go through what the bundle command did for us.
Besides basic stuff, like a
README.md files and initializing a GitHub repo in our directory we get:
Gemfile - well, we’ve all seen in it before, but here this file looks a bit differently:
As you can see, it’s practically empty. That’s because all our development dependencies go into the *.gemspec file, which is included here. If your gem needs to use some other gems to work after it’s included to a RoR app, you should put it into this Gemfile.
*.gemspec - this is where you add all of your gem’s development dependencies, and much more.
What is a development dependency? It’s what you use while creating the gem. Things like rspec, rake and bundler should be in this file, because those might not be necessary for the person adding this gem to his or hers Gemfile.
In this file you will need to declare the version of your gem as well. You can do it by referencing CreatingAGem module’s variable ‘VERSION’ (see line 8 in gist above).
Here you will also need to list all the files that should be included to your gem. By default the bundler uses git ls-files command, but if you don’t want to use git it’s up to you how you list them.
Rakefile sounds familiar as well.
Here, at the start it’s a simple one line file, where you require gem_tasks from the bundler.
Aaaaand finally the creatingagem.rb (
As you can see, your code goes there ;).
So, now you can start coding, you can test your gem and so on, but it still won’t be ‘tied’ to a Rails application if you add it to its Gemfile.
To extend Rails with your gem you will need to use Railtie, create a Railtie class which inherits from Rails::Railtie within your gem’s namespace. This class will be loaded during the Rails boot up.
Here you can see an example on how to do it:
Now you can use your gem with Rails! :)
Since you’re all set up to create your gem, you should know a couple of things about sharing what you create.
There is a gem that helps you version your gem. Check it out: gem-release. It lets you bump the version on any of those three numbers with a simple ‘bump’ command.
Everybody likes continuous integration, right? With services like Travis CI we can continuously integrate our gem. Thanks to the guys at Travis, you can build any open source project for free. To do so, you will need to register with Travis and select a public repo to build. Also you will need to configure your gem’s build. Here you can see a sample Travis configuration. It should be put inside ‘travis.yml’ file:
Finally, there is one last thing to do… Push the gem! :) To get it done, you’ll need to create an account on RubyGems.org. Once that’s finished, use your email and password when pushing the gem (RubyGems saves the credentials in ~/.gem/credentials for you so you only need to log in once).
After that it’s just a matter of making it available to other rubyists all over the world through rubygems.org. This is done with with two simple commands:
and once built:
gem push your_gem_name-0.0.1.gem
And that’s all it takes to create a gem! Have fun creating amazing new gems and share them with us! :)
We've collected our most commonly used gems in one place - make sure to take a look at Gem Glory - 10 Gems To Make Ruby Dev's Life Easier
On 20.06.2014 in Dev