62

I'm confused about what the gem naming convention is when the gem name has more than one word.

  • thinking-sphinx is the name of the gem, but the base *.rb file for this gem is lib/thinking_sphinx.rb (underscores)

  • acts-as-taggable-on is the name of the gem, and the base *.rb file is called lib/acts-as-taggable-on.rb (hyphens)

  • factory_girl uses an underscore in both the gem name and in the name of the base *.rb file

Does it matter if one uses underscores or hyphens? Is any emerging consensus here?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
dan
  • 43,914
  • 47
  • 153
  • 254

2 Answers2

54

Eric Hodel has a blog post on this: A Project Naming Recommendation

Rails solidified the convention of mapping CamelCase class names to underscored file names (class IMAPProcesor is defined in imap_processor.rb). Using underscored gem names makes it easy for people to figure out what file to require (same as the project name) or what class name to look for in ri.

If I have a plugin gem or an extension I’ll tack on the sub-project’s name with a dash. If I wanted to add a new handler for imap_to_rss for Chase bank email, the gem would be named imap_to_rss-chase.

Clint Pachl
  • 10,848
  • 6
  • 41
  • 42
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • 5
    This is consistene with the reccommendation given in the official Ruby Gems Guides -[Consistent Naming](http://guides.rubygems.org/patterns/#consistent-naming). – stevenharman Aug 03 '11 at 22:19
  • What about yajl-ruby or bcrypt-ruby, which are Ruby wrappers for existing C libraries? I do want a simple require 'bcrypt' inside my code, but I don't want to name my github project "yajl" or "bcrypt" because these (maybe) are already taken for the original C libraries. – dubek Aug 07 '11 at 07:52
  • 1
    This link to Eric Hodel's article is now dead. I tried to find it online, but after 3 google minutes didn't manage to do so. – gardenofwine Jul 08 '14 at 08:41
37

Following the advice here, here is a table of how things would break down:

Gem name Require statement Main class or module
fancy_require require 'fancy_require' FancyRequire
ruby_parser require 'ruby_parser' RubyParser
net-http-persistent require 'net/http/persistent' Net::HTTP::Persistent
rdoc-data require 'rdoc/data' RDoc::Data
autotest-growl require 'autotest/growl' Autotest::Growl
net-http-digest_auth require 'net/http/digest_auth' Net::HTTP::DigestAuth
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
user160917
  • 9,211
  • 4
  • 53
  • 63
  • I created a gem named tout_suite, but I still had to require it with `require 'tout/suite'`. Any ideas why? I'm using ruby 2.0 and rails 4.1.1 – gardenofwine Jul 13 '14 at 06:59
  • @gardenofwine, What was the main class or module? What did the file structure of your gem look like? Those things dictate what the gem 'should' be named, not the other way around. – user160917 Jul 13 '14 at 15:02
  • 1
    This just worked fine for me.. I created gem called calabash-shared and i had to require 'calabash/shared' - check this article for creating gems http://www.smashingmagazine.com/2014/04/08/how-to-build-a-ruby-gem-with-bundler-test-driven-development-travis-ci-and-coveralls-oh-my/ – Tejasvi Manmatha Jul 23 '14 at 16:09