且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails 3:引擎和 Gem 有什么区别?

更新时间:2023-02-01 08:30:24

Rails 术语中的 Engine 实际上是 Web 应用程序的子应用程序.例如,博客、论坛或简单的身份验证之类的东西:这些不是成熟的应用程序,而是可以添加到任何 Rails 应用程序的页面/视图/控制器/模型.

An Engine in rails terminology is a actually a subapplication of a web-application. For instance, something like a blog, a forum, or simple authentication: these are not full-blown applications, but pages/views/controllers/models that can be added to any rails application.

在 rails2 中,这将使用 plugin 来完成.现在,从 rails3 开始,引擎可以打包在 gem 中.

In rails2 this would be done using a plugin. Now since rails3 an engine can be packaged in a gem.

gem 是一个 ruby​​ 库,可以在 http://rubygems.org上找到a> 并且这是将 ruby​​ 代码打包并分发给其他 ruby​​ 人员的标准(唯一)方式.

A gem is a ruby library, which can be found on http://rubygems.org and it is the standard (only) way to package and distribute ruby code to other rubyists.

总结:

  • gem:是一个通用库,可以轻松安装,受版本管理,具有依赖项等.
  • 引擎:是 Rails 应用程序的子应用程序,从 Rails 3 开始,这些引擎作为 gem 分发(这太棒了!).

那么你什么时候会使用其中一个:

So when will you use one or the other:

  • 如果您想分享 ruby​​ 功能,请创建一个 gem
  • 如果您的 Rails 应用程序的某些部分可以更广泛地使用,请创建一个引擎(并将其打包到 gem 中).

希望这会有所帮助.