简体   繁体   English

Ruby Gem和Rails插件有什么区别?

[英]What is the difference between Ruby Gem and Rails plugin?

As far as I know, the only difference is in "installation". 据我所知,唯一的区别在于“安装”。 I've also heard that plugins are somehow harder to update. 我也听说插件在某种程度上更难更新。 But would I want to install something as a plugin instead of gem? 但我想安装一些插件而不是gem吗?

Gems: 宝石:

  • Can handle dependencies 可以处理依赖项
  • Can be installed system-wide 可以在全系统安装
  • Can also be vendored (in vendor/gems ) 也可以出售(在vendor/gems
  • Has a concept of versions, can easily specify which version you want 有一个版本的概念,可以轻松指定您想要的版本

Plugins: 插件:

  • Very easy to publish for authors 很容易为作者发布

Apparently, the only advantage for plugins is a powerful advantage, since plugins are still very common. 显然,插件的唯一优势是强大的优势,因为插件仍然很常见。 I tend to agree. 我倾向于同意。 Publishing a plugin = creating a github repository. 发布插件=创建github存储库。 Publishing a gem requires you to create a gemspec, and perhaps even meddle with The Gem Beast (rubyforge), which both are PITAs. 发布一个gem要求你创建一个gemspec,甚至可能干涉Gem Beast(rubyforge),它们都是PITA。

I'd say this question is likely to be debatable to which is the best approach but my thoughts none the less.. 我会说这个问题很可能是值得商榷的,这是最好的方法,但我的想法一点也不..

The biggest win I would say in using a gem over a plugin if given the option are gem dependencies. 如果给出选项,我会说在使用gem而不是插件时获得的最大胜利是gem依赖。

Rails plugins don't handle dependencies, so if you install a plugin that relies on other plugins/gems, you have the inconvenience of installing these additionally. Rails插件不处理依赖项,因此如果您安装依赖于其他插件/ gem的插件,则会额外安装这些插件带来的不便。 Where as installing a gem can at the same time pull in any dependencies required for it to run. 安装gem的同时可以引入运行所需的任何依赖项。 Take the cucumber gem for example: 以黄瓜宝石为例:

' sudo gem install cucumber ' will install the cucumber gem, but will also install its dependencies such as the webrat gem. ' sudo gem install cucumber '将安装黄瓜gem,但也将安装其依赖项,如webrat gem。

This may not seem like a big thing, but when you are dealing with multiple environments (development, staging, production etc.) you are more prone to encounter dependency issues. 这可能看起来不是什么大事,但是当您处理多个环境(开发,登台,生产等)时,您更容易遇到依赖性问题。

The only scenarios where you are likely to want a plugin over a gem is when you conveniently want access to the plugin code. 您可能想要访问gem的插件的唯一场景是您方便地想要访问插件代码。 One case may be if the plugin is infact a Rails engine and you need to quickly dive into the code and see what class/view you want to override. 一种情况可能是如果插件实际上是Rails引擎,您需要快速浏览代码并查看要覆盖的类/视图。 Another case may be if you have forked a plugin such as active_merchant, and have it setup as a git submodule in vendor/plugins. 另一种情况可能是你已经分叉了一个插件,比如active_merchant,并将它设置为vendor / plugins中的git子模块。 Committing and pushing changes upstream tends to be alot more convenient in this scenario. 在这种情况下,承诺和推动上游变化往往更加方便。

As for one being harder to update over the over, gems have rubygems to manage them which is straight-forward enough, and if you happen to be using git then submoduled plugins are a doddle to update: 至于一个更难以更新的东西,宝石有rubygems来管理它们,这很简单,如果你碰巧使用git,那么子模块插件是一个需要更新的doddle:

# installing a plugin as a git submodule
git submodule add git://path_to_plugin_git_repo.git vendor/plugins/plugin_name

# updating said git submodule
cd vendor/plugins/plugin_name
git remote update
git rebase origin/master
cd ../../..
git add .
git commit -m "update plugin_name to latest master."

or the equivalent for SVN externals 或SVN外部的等效物

Installing something as a gem or a plugin however shouldn't affect the functionality of the code as they are just two different ways of packaging code up, in the end it comes down to what you find more convenient to use I guess. 然而,作为gem或插件安装某些东西不应该影响代码的功能,因为它们只是两种不同的代码打包方式,最后它归结为你觉得使用起来更方便的东西。 Personally I find gems much easier to maintain unless I'm likely to be getting stuck into the code, then a submoduled plugin is more to my favour as it will show up in project tree in my editor. 我个人觉得宝石更容易维护,除非我可能会陷入代码,然后一个子模块插件更有利于我,因为它将出现在我的编辑器的项目树中。

I always use gems if possible because I feel it's much easier to keep them update. 如果可能的话,我总是使用宝石,因为我觉得让它们更新要容易得多。

However , one major annoyance is that gems can't provide rake tasks but plugins can. 然而 ,一个主要的烦恼是宝石不能提供佣金任务,但插件可以。 This will often result in frustration when, for example, you try to execute rake paperclip:refresh only to be informed that the task doesn't exist. 例如,当您尝试执行rake paperclip:refresh时,这通常会导致沮丧rake paperclip:refreshrake paperclip:refresh以通知任务不存在。

The solution is to copy the gem's rake tasks to your apps tasks directory. 解决方案是将gem的rake任务复制到您的apps任务目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM