42

I'm creating a ruby gem and I've noticed that there doesn't seem to be (to my knowledge) a naming convention for gems. For example, I've seen both:

gem 'foo-bar'
gem 'foo_bar'

Is there some kind of definitive guide/convention for the naming of ruby gems?

Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • 2
    possible duplicate of [Should one use dashes or underscores when naming a gem with more than one word?](http://stackoverflow.com/questions/4687659/should-one-use-dashes-or-underscores-when-naming-a-gem-with-more-than-one-word) – KL-7 Jan 22 '12 at 07:21
  • might help https://shivab.com/blog/ruby/2019/08/30/convert-your-ruby-script-to-a-ruby-gem/ – Shiva Sep 28 '19 at 05:28

4 Answers4

70

The dashed version is for extensions on other frameworks, like rspec-rails and the underscore is for part of the normal gem name and should be camelcased in your classes.

So if you have a gem named foo_bar, the class/module should be named FooBar. If that gem should have a rails extension which ships as a different gem, it should be called foo_bar-rails and the module should be called FooBar::Rails and it should be required as require "foo_bar/rails"

This convention is also what Bundler tries to require.

Admittedly, this convention is not always followed. jquery_rails should actually be jquery-rails and factory_girl_rails should be called factory_girl-rails. But hey, not everything is perfect.

RubyGems convention docs:

Clint Pachl
  • 10,848
  • 6
  • 41
  • 42
iain
  • 16,204
  • 4
  • 37
  • 41
  • 5
    This is bolstered by the rubygems patterns docs [here](http://guides.rubygems.org/patterns/). – turboladen Nov 08 '12 at 18:35
  • 1
    An rather old but interesting conversation about this topic can be found in the `bundler` GitHub repo itself: https://github.com/bundler/bundler/issues/1255 – thutt Aug 04 '17 at 15:04
5

Turns out that this is answered pretty clearly and succinctly in the rubygems docs: http://guides.rubygems.org/name-your-gem/

(This may be a recent doc addition because I recall searching for this info in the past and not finding it.)

tjstankus
  • 1,051
  • 11
  • 8
4

The one advantage is the convention of collapsing foo_bar into module or class FooBar as far as autoloaders go. foo-bar doesn't have a default equivalent.

Generally the underscore version is preferable from a require perspective, but the dashed version does come across as more readable so it tends to get used often.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 5
    The dashed version is more readable‽ It looks completely wrong to me, in what language can identifiers have dashes in them? – Nemo157 Jan 22 '12 at 09:15
  • @Nemo157 - e.i. in CLIPS. I used to program in it and having dashes in identifiers was a nice thing to have. You did not need to press to get "_" and you could write something like: purple-cow_painter-module, cat_painter-module (I used _ to more separate things than -) – Szymon Jeż Feb 15 '12 at 11:16
4

In a recommendation of @svenfuchs:

  • underscore => camelized
  • hyphen => name::space

https://twitter.com/svenfuchs/status/135773593526206464

But it's true that I still see non-coherence behaviors like:

gem 'my_gem`, :require => 'my-gem'

https://twitter.com/#!/svenfuchs/status/135784542819713024

fguillen
  • 36,125
  • 23
  • 149
  • 210