Gotchas for writing rubygems

2019-02-13 20:11发布

There have been questions with answers on how to write rubygems, but what should you avoid in writing a rubygem? What can cause problems for people using your rubygem?

标签: ruby rubygems
1条回答
狗以群分
2楼-- · 2019-02-13 20:48

Gem Packaging: Best Practices gives a lot of advice, some of which include

  • Don't pollute the global load path. Ideally, only have foo.rb in your lib directory, and put all your other files in lib/foo.

  • Don't require files using __FILE__.

  • Don't rely on anything outside the load path. Folders may not have the same structure as in your original version. For example, don't use something like

    VERSION = ::File.read(::File.join(::File.dirname(FILE), "..", "..", "VERSION")).strip

  • Don't manage $LOAD_PATH within lib.

  • Provide a VERSION constant.

  • Don't depend on rubygems. The person using your code may not be using rubygems, but some other packaging system (or no packaging system). Similarly, don't mention version dependencies in the code itself, or rescue Gem::LoadError.

Rubygems dependencies. Please... argues that you shouldn't list optional runtime dependencies, and should separate developer from runtime dependencies.

From my own experience: if nothing else, try building and installing your gem locally before releasing it into the wild. It avoids brown paper bag releases.

查看更多
登录 后发表回答