35

To create a new Ruby gem, should I use Jeweler or should I use Bundler's built-in gem skeleton to create a base gem? What are the differences that matter?

4

6 Answers 6

60

Use Bundler

From the command line:

bundle gem your_new_gem

This will create a directory called your_new_gem with just a basic set of files and directory structure that are now considered best-practice. It's quick, easy, and a great place to start.

7

Creating a Gem isn't that difficult and I would advise to try building a gem from scratch, without any tools. After you know what's involved (creating a gemspec, building and pushing it to rubygems.org), you can use tools to speed up the process. My guess is you won't because making a gem is hardly the trouble at all.

4
  • Good point. It is better to understand how to do it from scratch before you use a generator. This will make it alot easier to debug any problems that arise if you decide to use Jeweler. I do think that Jeweler makes it alot easier to create a gem and it forces you to do things like creating a description, versioning, etc.. why would you create it from scratch (after you do it once to learn it), when you can use a Jeweler and speed the process? Waste of time in my opinion, and less prone to errors. Dec 5, 2010 at 19:35
  • 1
    sorry I'm late: What jeweler creates isn't easier, it just moves the stuff to the Rakefile. A gemspec is only about 10 lines. I like the stuff bundler generates though. Sensible and minimal.
    – iain
    Dec 9, 2010 at 21:57
  • 3
    Learning to write a gem from scratch instead of using a generator is like writing a web framework from scratch without using rails new. I see your point about creating one from scratch, but for someone new to creating gems (or anything for that matter), sometimes it's easier to learn by example. When you see what the generator builds, you can learn what each piece does and why it is important to the structure. Once you understand how it works, then you can write one from scratch.
    – Andrew
    Jul 16, 2011 at 5:54
  • In that case, I would go for the generator provided by bundler gem.
    – iain
    Jul 17, 2011 at 11:34
3

I would go with Jeweler. The Bundler skeleton is only going to give you the basics. Jeweler has alot more options to work with and many helpful rake tasks for versioning, pushing to github, creating the gemspec, building and installing.

If you are working with Rails 3 engines, I have a Jeweler fork (definitely a work-in-progress) that will generate the app skelaton and include the engine file. You just have to run the jeweler command with --rails3-engine as an option. Here is the fork if you are interested:
https://github.com/johnmcaliley/jeweler

2
  • 1
    Jeweler is no longer kept up-to-date as much as Bundler, as the author confesses that most people now use Bundler instead: github.com/technicalpickles/jeweler/issues/232
    – Jon Cairns
    Feb 15, 2013 at 8:58
  • I agree. I no longer use jeweler. It is probably better to start from scratch your first time, so you understand how the gemspec works. Feb 15, 2013 at 14:39
2

I would recommend using the built-in bundler command.

bundle gem your_gem_name

There are some rules that you should follow when creating a gem. Such as naming conventions and versioning rules. I recently wrote a post on creating gems in netguru's blog. I think you'll find what you need in there.

https://netguru.co/blog/posts/creating-a-gem-a-step-by-step-tutorial

Hope this helps.

1
1

Here's an alternative that's worth looking at: ore

Bundler gives you a single template for ruby gems, whereas ore has multiple built in templates, plus the ability to create your own. It also supports Git, SVN (urgh) and Mercurial.

1

You can build a gem in RubyMine too. File > New Project > New Gem. It is that easy. But I want to make some notes about this approach:

For debugging, RubyMine will use the Fast Debugger gem, ruby-debug-ide. I know that most people now are using Pry with Byebug, but ruby-debug-ide is an interface which glues ruby-debug to IDEs like Eclipse (RDT), NetBeans and RubyMine.

Under Run > Edit Configurations > + > Ruby, I add a new debug configuration, according to the documentation here: https://www.jetbrains.com/help/ruby/run-debug-configuration-gem-command.html#1

Under Configuration, under 'Ruby Script', I add the path to the ruby gem file under lib: lib/my_gem.rb Under Configuration, under 'Ruby SDK', I specify an RVM gemset I am using. Under Bundler section, I check 'Run the script in context of bundler'. This would use bundle exec, which will read the dependencies in my Gemfile in my project's root. Now for gems, the Gemfile contains a method call "gemspec", which in turn reads the dependencies in dependencies in my_gem.gemspec. There, I have dependencies passed to the Gem::Specification.new block:

  spec.add_development_dependency "bundler", "~> 1.7"
  spec.add_development_dependency "rake", "~> 10.0"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.