Ruby-on-rails Gemfile在rails中有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14072880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of Gemfile in rails?
提问by vijay
What is the use of Gemfilein rails?
Gemfile导轨有什么用?
How to use Gemfile?
如何使用Gemfile?
回答by Nick Ginanto
During your development in Rails, there will be times where you will want to provide some functionality which is required by you, but either you don't know how to do or you don't want to implement it on your own since a lot of work has been put into its development by talented developers.
在 Rails 开发过程中,有时您会希望提供一些您需要的功能,但是您要么不知道该怎么做,要么不想自己实现,因为很多有才华的开发人员已将工作投入到其开发中。
These developments which you might need (user authentication, message system, asset handlers, geolocation, pagination system, linking to exterior services such as Amazon AWS, and last but not least Rails itself) are called Ruby Gems. These are ruby software packages, not necessarily relating to Rails, but since Rails is based on Ruby, 98% of the gems can be made availble to your Rails webapp code.
您可能需要的这些开发(用户身份验证、消息系统、资产处理程序、地理定位、分页系统、链接到外部服务,例如 Amazon AWS,以及最后但并非最不重要的 Rails 本身)被称为 Ruby Gems。这些是 ruby 软件包,不一定与 Rails 相关,但由于 Rails 基于 Ruby,因此 98% 的 gems 可用于您的 Rails webapp 代码。
Lots of gems can be found in github, but its funner to search for gems via ruby-gemsor ruby-toolbox
在github 中可以找到很多 gem ,但是通过ruby-gems或ruby-toolbox搜索 gem 更有趣
Your gemfileis a list of all gems that you want to include in the project.
It is used with bundler(also a gem) to install, update, remove and otherwise manage your used gems.
您gemfile是要包含在项目中的所有 gem 的列表。它与bundler(也是一个 gem)一起使用来安装、更新、删除和以其他方式管理您使用的 gem。
The gemfilehas another purpose - you can group gems in :development, :test, :assets, :production, etc groups and Rails will know when to include the gems. For example:
在gemfile还有另一个目的-你可以组宝石:development,:test,:assets,:production,等团体和Rails会知道什么时候包括宝石。例如:
group :development, :test do
gem "rspec-rails"
gem "factory_girl_rails"
gem "guard-rspec"
end
Note that on Rails 4, the assetsgroup has been deprecated
请注意,在 Rails 4 上,该assets组已被弃用
These gems belong to development environment and the test environment since they are for testing the application. You don't need them available in the production environment (you could, but that will bloat the memory unnecessarily).
这些 gem 属于开发环境和测试环境,因为它们用于测试应用程序。您不需要它们在生产环境中可用(您可以,但这会不必要地增加内存)。
So - To use the gemfile, simply write the gem you wish to install such as
所以 - 要使用gemfile,只需编写您希望安装的 gem,例如
gem 'devise'
make sure to install bundlerbeforehand (in your console/cmd/ssh) with
确保bundler事先安装(在你的控制台/cmd/ssh 中)
$ gem install bundler
and then write in the console
然后在控制台写
bundle install
you will notice another gemfile appears! Gemfile.lockThis file, as you will see if you open it with a text reader, lists all your gems with their version and their dependencies. This will come useful when you need to know which versions of the gems you installed.
你会注意到另一个 gemfile 出现了!Gemfile.lock如果您使用文本阅读器打开该文件,您将看到该文件列出了所有 gem 及其版本和依赖项。当您需要知道您安装了哪些版本的 gem 时,这将非常有用。
For more reading on the Gemfile- read on the bundler page
有关Gemfile-阅读捆绑器页面上的更多阅读
for information regarding picking a gem you could start with this
有关挑选宝石的信息,您可以从这里开始
Good luck and have fun!
祝好运并玩得开心点!
Ok, so whats this Gemfile.lock that got created?
好的,那么创建的这个 Gemfile.lock 是什么?
Gemfile.lock, as the name suggests is a locking on all the versions of all the gems that got installed. So if Gemfile is what required to be installed, the lock file is what got installed and what version are actually required to get the app up and running.
Gemfile.lock,顾名思义是对所有已安装 gem 的所有版本的锁定。因此,如果 Gemfile 是需要安装的文件,那么锁定文件就是安装的文件以及启动和运行应用程序实际需要的版本。
If you don't have the gems in that specific version (as specified in Gemfile.lock) rails will complain and you will have to either install the missing gems (via bundle install) or fix any conflicts manually (I believe bundler will give you some clues on that)
如果您没有该特定版本中的 gem(如 Gemfile.lock 中指定的),rails 会抱怨,您将不得不安装丢失的 gem(通过bundle install)或手动修复任何冲突(我相信 bundler 会给您一些线索在那)
Some things to know about Gemfile.lock
一些需要了解的事情 Gemfile.lock
- if you accidently delete it, it will get regenerated when you run
bundle install. If you accidently deleteGemfile, you are out of luck.. You should use git :) - Heroku doesn't care about Gemfile.lock since it will reinstall all gems. So for Heroku, you mustset the gem version you want, or Heroku will always install the latest version of gem, which may cause issues
- Keep the Gemfile.lock in your project so you will always know what version of gems make your app work properly.
- 如果您不小心删除它,它会在您运行时重新生成
bundle install。如果你不小心删除了Gemfile,你就不走运了..你应该使用 git :) - Heroku 不关心 Gemfile.lock,因为它会重新安装所有的 gem。所以对于Heroku,必须设置自己想要的gem版本,否则Heroku会一直安装最新版本的gem,可能会出现问题
- 将 Gemfile.lock 保留在您的项目中,以便您始终知道什么版本的 gems 使您的应用程序正常运行。
回答by willglynn
Gemfiles are configuration for Bundler, which is used to manage your application's Ruby dependencies. That website includes a lot of documentation, including the Gemfile manual page.
Gemfiles 是Bundler 的配置,用于管理应用程序的 Ruby 依赖项。该网站包含大量文档,包括Gemfile 手册页。
回答by BKSpurgeon
I'll try and give a simple answer
我会试着给出一个简单的答案
Explanation by analogy
类比说明
Let's suppose you want to make a car. From scratch. Now that will be a nightmare. You need to build: the chasis, the engine, the corroborator, radiator, cooling, ventilation, etc etc. etc.
假设您想制造汽车。从头开始。现在那将是一场噩梦。您需要建造:底盘、发动机、验证器、散热器、冷却、通风等等等。
Gems allow you to utilise car parts which other people have made before
宝石允许您使用其他人以前制造的汽车零件
But you're not the only one building a car. Everyone's who's ever built a car has basically made the same things. There have been smart people who've gone out there and made combustion engines etc well before you were...........actually never mind.
但你并不是唯一一个制造汽车的人。每个造过汽车的人基本上都造过同样的东西。有一些聪明人在你之前就已经走出去并制造内燃机等......实际上没关系。
You needn't reinvent the wheel. Why make your own when you can simply get the best equipment straight off the shelf? What if you could get one of the best engines around, created by the most talented engineers in the world, without lifting a finger? Are you gonna spend a year trying to make your own?
你不需要重新发明轮子。当您可以直接从货架上获得最好的设备时,为什么要自己制造呢?如果您无需动动手指就能获得由世界上最有才华的工程师创造的最好的引擎之一,那会怎样?你会花一年时间尝试自己制作吗?
So basically rather than make everything yourself, you write down a shopping list of all the parts you need. And that is basically a gem file:
因此,基本上不是自己制作所有东西,而是写下您需要的所有零件的购物清单。这基本上是一个 gem 文件:
- Rolls Royce Engine
- AutoLive seatbelts
- Michellin tyres.
- PIAA Night headlights
- etc etc.
- 劳斯莱斯发动机
- AutoLive 安全带
- 米其林轮胎。
- PIAA 夜间大灯
- 等等等等
That my friend, is basically your gem file!
我的朋友,基本上就是你的 gem 文件!
回答by Rahul garg
Your system can have lots of gems ... thus can have multiple versions of same gem.
你的系统可以有很多 gems ......因此可以有多个版本的相同gem.
A Gemfilespecifies the list of gemswith their versionsthat shall be used/loaded/(install if not present) whenever you run your rails application. or anything with bundle exec . .
一个Gemfile指定的名单gems与他们versions每当你运行你的Rails应用程序,应使用/加载/(安装如果不存在)。或任何与bundle exec . .
回答by slal
Firstly, what is a gem?
首先,什么是宝石?
According to Wikipedia:
根据维基百科:
RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries
RubyGems 是 Ruby 编程语言的包管理器,它提供了用于分发 Ruby 程序和库的标准格式
Gemfile
文件
A Gemfile is a file we create which is used for describing gem dependencies for Ruby programs
Gemfile 是我们创建的文件,用于描述 Ruby 程序的 gem 依赖项
Now, in very very simple words:
现在,用非常非常简单的话来说:
Gem can be thought of as a library which you can use in your code. Example: faker gem
Gem 可以被认为是一个可以在代码中使用的库。示例:假宝石
Your code can use the functionality of fakergem to produce fake data.
您的代码可以使用fakergem的功能来生成假数据。
Now you can list all the gems that your project requires in the gemfile. When you do a bundle install, all the gems in your gemfile are installed for you.
现在您可以在 gemfile 中列出您的项目需要的所有 gem。当您进行捆绑安装时,您的 gemfile 中的所有 gem 都会为您安装。

