在Gemfile和.ruby-version Dotfile中都列出Ruby版本是一种不好的做法吗? [英] Is It A Bad Practice to List Ruby Version in Both Gemfile and .ruby-version Dotfile?

查看:144
本文介绍了在Gemfile和.ruby-version Dotfile中都列出Ruby版本是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最新的Rails项目或多或少,并尝试让我打破很多东西并在此过程中学习.我在gemfile中指定了最新版本的Ruby:

My latest Rails project is more or less and experiment for me to break lots of things and learn in the process. I have the latest version of Ruby specified in my gemfile:

ruby '2.2.3'

我在项目中还有一个.ruby-version点文件,其内容如下:

And I also have a .ruby-version dotfile in the project, with the following contents:

2.2.3

除了明显的重复之外,这又有什么问题呢?两种约定的目的是什么?如果我只有一个约定来列出我的Ruby版本,为什么我应该有一个约定(Gemfile)而不是另一个约定(dotfile)?

Other than the obvious duplication, what is wrong with this? What is the purpose of both conventions? If I should only have one convention for listing my Ruby version, why should I have one (Gemfile) over the other (dotfile)?

在项目中同时使用两个约定是否完美?

Is it perfectly fine to have both conventions in a project?

我将成为该实验项目的唯一维护者,并且不觉得必须维护这一细微重复是一个问题.我不打算为该项目升级Ruby,如果我这样做了,我会记得在两个地方都这样做不会有问题.除了这些细节之外,我当然避免在我的应用程序的代码库中进行任何此类重复.

I am going to be the only maintainer of this experimental project, and dont feel it will be an issue having to maintain this one subtle duplication. I dont intend to upgrade Ruby for this project, and if I do, I wont have an issue remembering to do it in both places. Other than this detail, I certainly avoid any such duplication in my applications' codebases.

推荐答案

它们分别由不同的团队在不同的时间开发,并由不同的软件使用.

They were each developed by different teams at different times, and are used by different software.

在宝石文件中列出红宝石版本是捆绑程序中的功能.

Listing a ruby version in the Gemfile is a feature in bundler.

由于Gemfile主要仅由捆绑软件使用,因此它仅在使用捆绑软件运行时才会生效-使用bundle exec或自动为您触发捆绑软件的软件(如Rails).如果您未使用指定的ruby版本,它的作用就是出错并拒绝运行.这是一个要求-在此红宝石下运行,否则我将抛出错误警告,说明您在错误的红宝石下运行.

As the Gemfile is mostly only used by bundler, it will mainly only effect things when you run the with bundler -- using bundle exec, or software (like Rails) that automatically triggers bundler for you. It's effect is simply to error out and refuse to run if you aren't using the version of ruby specified. It's a requirement -- run under this ruby, or I'll throw an error warning you you're running under the wrong ruby.

但是, heroku也会注意 Gemfile中指定的版本,并将在该版本下运行. Heroku也决定在捆绑程序中使用该功能.但是,您工作站上的大多数其他软件,甚至甚至 travis 都不使用该约定. (Travis使您可以编辑.travis.yml ENV以指定要使用的红宝石版本).

However, heroku also pays attention to the version specified in the Gemfile, and will run under that version. Heroku decided to use the feature in bundler too. But most other software, on your workstation, or even travis, does not use that convention. (Travis makes you edit your .travis.yml ENV to specify ruby version to use).

捆绑程序中的功能于2012年8月在Bundler 1.2中引入.

The feature in bundler was introduced in Bundler 1.2 in August 2012.

rvm ,第一个ruby版本管理器.如果您使用的是rvm,并且切换到带有.ruby-version文件的项目目录,则rvm将自动将您的Shell切换为使用指定的ruby版本.

The .ruby-version file was first introduced by rvm, the first ruby version manager. If you are using rvm, and you switch into a project direcotry with a .ruby-version file, rvm will automatically switch your shell to using the ruby version specified.

我不确定rvm何时引入了此功能,但我认为在Gemfile的红宝石"功能之前.

I'm not sure when rvm introduced this feature, but I think before the Gemfile "ruby" feature.

自从rvm引入以来,其他的ruby版本切换软件(例如rbenv和chruby)也采用了它来做同样的事情-当您将cd进入目录时,会自动切换到指定的ruby版本.尽管我认为rbenv和chruby都可能是可选功能.

Since rvm introduced it, other ruby version switching software like rbenv and chruby have adopted it too to do the same thing -- automatically switch to the ruby version specified when you cd into the directory. Although I think with rbenv and chruby both it may be an optional feature.

因此,它们是在不同时间引入并受不同软件包支持的不同功能,它们的功能有所不同.

So they were different features introduced into and supported by different software packages at different times, doing somewhat different things.

我同意同时维护两者并保持同步会很烦人.

I agree it would be annoying to maintain both, and keep them in sync.

它们实际上都是可选的,您不需要使用任何一个.除了您可能需要对heroku使用Gemfile ruby​​规范,以告诉它要运行哪个ruby.

They are both actually optional, you don't need to use either one. Except that you might need to use the Gemfile ruby spec for heroku, to tell it which ruby you want it to run.

我个人都不使用.但是,如果您需要在不同的项目中使用不同的ruby版本,并且发现将ruby版本管理器(rvm,rbenv或chruby)自动切换到正确的项目特定的ruby版本很方便,则.ruby-version可能会有用.

I don't personally use either. But if you need to work in different ruby versions in different projects, and find it convenient to have your ruby version manager (rvm, rbenv, or chruby) automatically switch to the right project-specific ruby version, .ruby-version might be useful.

除了出于Heroku的目的外,在Gemfile中列出红宝石主要是为了防止自己出错,例如在部署时.也许内部自动部署或CI环境可以像heroku一样使用它们,或者其他云部署堆栈也可能或已经采用了它.我认为很多人都觉得它没什么用,但我也不会用,除非您遇到或看到它可以解决的问题.某些人在Gemfile中列出红宝石版本的不便之处在于,总是有新的红宝石出现,因此您必须一直更新所有Gemfile.

Except for heroku purposes, listing ruby in the Gemfile is mostly just to keep yourself from making a mistake, for instance on deployment. Or perhaps an in-house automated deployment or CI environment could use them somewhat like heroku does, or perhaps other cloud deployment stacks will or have adopted it. I think many have found it not too useful though -- this one too, I wouldn't use until you run into or see a problem that it's solving. One inconvenience some people have with listing ruby versions in Gemfile is that with new rubies always coming out, you've got to be updating all your Gemfiles all the time.

通常,过去几年的ruby版本都向后兼容,从而限制了确保使用精确版本的ruby的需要,即使最新代码也可以在最新的ruby上运行最初是为年龄较大的人编写的.

In general, the last couple years of ruby releases have all been very backwards compatible, limiting the need to be sure you're using an exact version of ruby, most recent code will run on the most recent ruby, even if it was originally written for an older one.

我认为这两个功能都不会让您指定一系列的红宝石版本,例如2.2.*或您拥有的版本.

I don't believe either feature lets you specify a range of ruby versions, like 2.2.* or what have you.

具有任一功能/全部功能,仅在需要它们或发现它们有用时才使用它们,而不必使用任何一个,并且如果需要两者兼用,则可以很好地使用(如果烦人).

With either/both features, use them only if you need them or find them useful, you don't have to use either, and it's fine (if annoying) to use both, if you need what both do.

由于Gemfile是实时红宝石代码,因此从理论上讲,您可以让Gemfile读取您的.ruby-version文件,并自动将该值用作Gemfile ruby值.如果您想同时使用它们和不要重复自己".我不知道这是否是一件普通的事情,我只是想到了这一点.但它应该可以正常工作.

Since a Gemfile is live ruby code, you could theoretically have your Gemfile read your .ruby-version file and automatically use that value as the Gemfile ruby value. If you wanted to use both, and "don't repeat yourself" with it. I don't know if that's a common thing to do, I just thought of it. But it should work fine.

这篇关于在Gemfile和.ruby-version Dotfile中都列出Ruby版本是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆