We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 9 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
一般来说,Ruby gem 非常简单,Rails 插件只是以某种方式接触 Rails API 的 gem。 (正如另一个答案所指出的,Railtie 是挂钩 Rails 的好方法)。要编写 gem,请指定一个文件夹,并在其中包含:
.gemspec
:这列出了 gem 的名称、版本、作者、网站、gem 依赖项列表和文件列表包含在宝石中。 (Rails 的plugin new
和 Bundler gem 都会为您生成良好的 .gemspecs,请参见下文)lib/.rb
:这是您的主要代码宝石。通常这包含一个以您的 gem 命名的类或模块。 (Rails 的plugin new
与模块一起使用)。其他一切都是可选的,但良好的做法包括:
test/
或spec/
:这是您的测试所在。由于 Rails 插件通常在应用程序中使用,Rails 的plugin new
在test/
中创建了一个虚拟应用程序。如果您的 gem 非常独立,没有显式的 Rails 集成,则不需要虚拟应用程序。您可以完全手动创建此文件夹,以及更重要的是
.gemspec
文件,但这两个工具将为您提供以下功能:Bundler 为您做的事情:
Gemfile
并自动将.gemspec
中指定的依赖项包含在捆绑包中(它是单个Gemfile
命令,gemspec
).gemspec
为您设置从lib//version.rb
提取您的 gem 版本,Rails 的
plugin new
执行Gemfile
和.gemspec
,就像 Bundler 一样。另外:Rails 还会为您创建一个 Rakefile,用于添加 rake 任务,并且这两个工具都会为 Git 预先配置一个 .gitignore 以忽略一些常见产品。
要在实际应用中测试您的 gem,请执行
gem build.gemspec
,并将gem
添加到应用的Gemfile
。这将创建一个 -.gem 文件,其中包含整个 gem。您可以gem install-.gem
,Bundler 会选择它。当您准备好发布时,
gem push-.gem
会将其推送到 RubyGems.org。有关获取帐户和推送 gem 的详细信息,请参阅 http://rubygems.org。另外,在开发过程中,您会想要对 gem 进行更改,而不必继续构建(和安装)版本。单元测试将解决其中的一些问题,但如果您想直接从源代码使用 gem,Bundler 有一个很棒的工具:
gem, :path =>; ""
该文件夹是包含
.gemspec
文件的文件夹。有人知道使用珠宝商的理由吗?我比这些人更想知道它的作用。一般来说,一旦你有了一个可以复制、粘贴和修改的模板,手动编写
gemspec
对我来说就非常容易了。另外,如果我在每个工具为您提供的内容中犯了任何错误或遗漏,我已将答案制作为 wiki,以便可以更正它们。Generally, Ruby gems are pretty simple, and Rails plugins are just gems that in some way touch Rails APIs. (as the other answer pointed out,
Railtie
s are a good way to hook into Rails). To write a gem, designate a folder, and have in it:<gemname>.gemspec
: this lists the gem's name, version, authors, website, a list of gem dependencies and a list of files to include in the gem. (Rails'plugin new
and Bundler gem will both generate good .gemspecs for you, see below)lib/<gemname>.rb
: this is the main code of your gem. Usually this contains a class or a module named after your gem. (Rails'plugin new
goes with module).Everything else is optional, but good practices include:
test/
orspec/
: this is where your tests go. Because a Rails plugin is usually used in an app, Rails'plugin new
makes a dummy app intest/
. If your gem is pretty standalone, with no explicit Rails integration, you don't need the dummy app.You can make this folder and, critically, the
.gemspec
file entirely by hand, but the two tools will give you the following:Bundler does for you:
Gemfile
for you and automatically includes dependencies specified in your<gemname>.gemspec
in the bundle (it's a singleGemfile
command,gemspec
)<gemname>.gemspec
for you, sets it up to pull your gem's version fromlib/<gemname>/version.rb
Rails'
plugin new
does theGemfile
and.gemspec
, just like Bundler. Also:Rails will also make you a
Rakefile
, for adding rake tasks, and both tools will preconfigure a .gitignore for Git to ignore some common products.To test your gem in an actual app, do
gem build <gemname>.gemspec
, and addgem <gemname>
to your app'sGemfile
. This will make a -.gem file, which contains the whole gem. You cangem install <gemname>-<version>.gem
, and Bundler will pick it up.When you're ready to publish,
gem push <gemname>-<version>.gem
will push it up to RubyGems.org. See http://rubygems.org for details on getting an account and pushing gems.Also, while you're developing, you'll want to make changes to your gem and not have to keep building (and installing) versions. Unit testing will take care of some of that, but if you'd like to use your gem directly from source, Bundler has a fantastic tool:
gem <gemname>, :path => "<path/to/your/gems/folder>"
The folder is the one that contains the
.gemspec
file.Does anyone know any reason to use jeweler? I'd love know what it does more than these guys. Generally, writing
gemspec
s by hand is pretty easy for me, once you've got one template to copy-and-paste-and-modify. Also, if I made any mistakes or omissions in what each tool gives you, I've made the answer a wiki so they can be corrected.rails plugin new
创建一个空的 gem,它会立即挂接到 Rails 中。因此,它确实创建了一个“框架”供您填写。然后您仍然需要
bundler
或jeweler
来实际将这组文件转换为 gem 并发布它。您从这两者中选择哪一个有点取决于品味。最近我发现很多人实际上都在提倡使用
bundler
。但对于bundler
来说,它仍然主要是一个手动过程。我个人认为使用
jeweler
还是很有趣的。使用 Jeweler 的优点是什么:Gemfile
,Jeweler 将生成与其相对应的 gemspec。请注意,这与来自打包程序的 Yehuda 的建议相反:他建议手动填写 gemspec,然后通过在文件中指定gemspec
来让Gemfile
使用它。简而言之,在完成了
rails plugin new
之后,我仍然建议使用jeweler
。希望这有帮助。
rails plugin new
creates an empty gem, that immediately hooks into rails. So it does creates a "frame" for you to fill in.You will then still need either
bundler
orjeweler
to actually convert this set of files to a gem and publish it.Which you would choose from the two is a bit a matter of taste. Lately I find a lot of people are actually advocating to use
bundler
. But withbundler
it still is mainly a manual process.I personally believe it still is very interesting to use
jeweler
. What are the advantages of using jeweler:Gemfile
as in any rails or other project, jeweler will generate a gemspec that corresponds to that. Note that this is opposite to what Yehuda from bundler fame proposes: he proposes to fill in the gemspec manually, and then lets theGemfile
use that by specifyinggemspec
in the file.In short I still recommend to use
jeweler
, after doing arails plugin new
.Hope this helps.
由于 gem 更易于安装和管理,因此最好使用
jeweler
来创建您的 gem。寻找灵感的最好方法是打开任何集成了 Rails 的 gem 的源代码,看看他们是如何做到的。一般来说,这是通过 Railties 设施完成的。Since gems are easier to install and manage, it might be best to use
jeweler
to create your gem. The best way to find inspiration is to open up the source of any gem that has Rails integration and see how they do it. Generally this is done through the Railties facility.