这里是一些命名gem的最佳实践
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 |
这个表格的主要目的就是为你提供在你的gem里面require文件的线索。尊从这个最佳实践也可以使得你Bundler加载gem无需你额外的配置。
如果你发布了一个gem到 rubygems.org
但是他令人讨厌,违反最佳实践或者反人类,那么他将不久于人世。你可以报告给 RubyGems Support site.
一、在多个单词之间使用下划线
如果一个class or module有多个单词,使用下划线分割他们。使用户使用gem更简单。
二、为扩展使用破折号
如果你添加函数到其他gem,使用破折号。他也和require语句中的/和main class or module 中::
的保持一致。
If you’re adding functionality to another gem, use a dash. This usually corresponds to a /
in the require statement (and therefore your gem’s directory structure) and a ::
in the name of your main class or module.
三、适当的混用下划线和破折号
如果你的class or module有多个单词。同时你也添加了函数到其他gem,那么请遵循以上规则。例如
net-http-digest_auth
添加了 HTTP digest authentication 到 net/http
.用户会require 'net/http/digest_auth'
以便使用扩展 (in class Net::HTTP::DigestAuth
)。
四、千万别使用大写字母
OS X and Windows的文件系统默认区分大小写(case-insensitive)。如果用户在非OS X and Windows系统下使用大写字母加载一个gem是不可移植的。这是一个新手常犯的错误,我们没必要被他迷惑。
五、尾声
This guide was expanded from How to Name Gems by Eric Hodel.