51

Gemfile铁轨有什么用?

如何使用Gemfile

4

5 回答 5

93

在 Rails 开发过程中,有时您会想要提供一些您需要的功能,但是您不知道该怎么做,或者您不想自己实现它,因为很多才华横溢的开发人员已将工作投入到它的开发中。

您可能需要的这些开发(用户身份验证、消息系统、资产处理程序、地理定位、分页系统、链接到外部服务(如 Amazon AWS)以及最后但并非最不重要的 Rails 本身)称为 Ruby Gems。这些是 ruby​​ 软件包,不一定与 Rails 相关,但由于 Rails 基于 Ruby,98% 的 gem 可用于 Rails webapp 代码。

在github中可以找到很多 gem ,但是通过ruby ​​-gems或ruby ​​-toolbox 搜索 gem 更有趣

gemfile是您想要包含在项目中的所有 gem 的列表。它与bundler(也是一个 gem)一起使用来安装、更新、删除和以其他方式管理您使用的 gem。

gemfile有另一个目的 - 您可以将 gem 分组到, :development, :test, :assets, :productionetc 组中,Rails 会知道何时包含这些 gem。例如:

group :development, :test do
    gem "rspec-rails"
    gem "factory_girl_rails"
    gem "guard-rspec"
end

请注意,在 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).

So - To use the gemfile, simply write the gem you wish to install such as

gem 'devise'

make sure to install bundler beforehand (in your console/cmd/ssh) with

$ gem install bundler

and then write in the console

bundle install

you will notice another gemfile appears! Gemfile.lock This 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.

For more reading on the Gemfile - read on the bundler page

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, 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.

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)

Some things to know about Gemfile.lock

  • if you accidently delete it, it will get regenerated when you run bundle install. If you accidently delete Gemfile, 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 must set 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.
于 2012-12-28T17:45:38.610 回答
11

Gemfiles 是Bundler的配置,用于管理应用程序的 Ruby 依赖项。该网站包含大量文档,包括Gemfile 手册页

于 2012-12-28T17:05:16.243 回答
4

Explanation by analogy

You want to build a car. From scratch. You need to build: a chasis, engine, corroborator, radiator etc.

Gems allow you to utilise car parts which other people have made before

Everyone's who's ever built a car has needed the same things.

You needn't reinvent the wheel. Why make your own engine etc when you can get it 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:

  • Rolls Royce Engine
  • AutoLive seatbelts
  • Michellin tyres.
  • PIAA Night headlights
  • etc etc.

That my friend, is basically your gem file!

于 2016-03-17T12:07:37.023 回答
1

您的系统可以有很多宝石......因此可以有多个版本相同的gem.

AGemfile指定当您运行您gemsversionsrails 应用程序时应使用/加载/(如果不存在则安装)的列表。或任何与bundle exec . .

于 2012-12-28T17:09:42.267 回答
0

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

Gemfile

A Gemfile is a file we create which is used for describing gem dependencies for Ruby programs

Now, in very very simple words:

Gem can be thought of as a library which you can use in your code. Example: faker gem

Your code can use the functionality of faker gem to produce fake data.

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.

于 2017-05-11T01:44:03.933 回答