简体   繁体   English

关于rvm的说明以及滑轨之间的切换

[英]clarification on rvm and switching between rails

following http://railsapps.github.io/installrubyonrails-mac.html , I encounter the following commands http://railsapps.github.io/installrubyonrails-mac.html之后 ,我遇到以下命令

rvm use ruby-2.1.3@rails4.1 --create
gem install rails  # installs the latest rails version
rails -v # returns 4.1.6

however, I can also do the following, which would add rails version to 4.0.8 但是,我也可以执行以下操作,将Rails版本添加到4.0.8

rvm use ruby-2.1.3@rails4.0 --create
gem install rails --version=4.0.8 # installs the latest rails version
rails -v # returns 4.0.8

What is the point of this? 这有什么意义呢? Somewhere in the text it's said that this method is to prevent global gem-set and instead install rails based on project-specific gemsets? 在本文中的某个地方,有人说这种方法是为了防止全局gem-set,而是根据项目特定的gemset安装rails? What does that even mean? 那有什么意思?

And this is the instructions on how to create a new rails project 这是有关如何创建新的Rails项目的说明

$ mkdir myapp
$ cd myapp
$ rvm use ruby-2.1.3@myapp --ruby-version --create
$ gem install rails
$ rails new .

why not just call rails new myapp? 为什么不只将Rails称为myapp? The text says it's to "create a project-specific gemset", but I have no idea what that means. 文字说这是“创建特定于项目的gemset”,但我不知道这是什么意思。 Wouldn't this just install rails 4.1.6 (newest version) ? 这不是只安装Rails 4.1.6(最新版本)吗? why not just install rails 4.1.6 globally in the first place then? 为什么不首先在全球范围内安装Rails 4.1.6?

Imagine you are a Rails developer in a company that has been doing Rails apps for the last 4 years. 想象一下,您是一家在过去四年中一直在开发Rails应用程序的公司中的Rails开发人员。 You have apps on Rails 2, Rails 3, Rails 4 - as new versions come out, you upgrade your toolset because, why not? 您在Rails 2,Rails 3,Rails 4上都有应用程序-随着新版本的发布,您升级了工具集,因为为什么不呢? Each new version is better. 每个新版本都更好。

However, they are not downward compatible. 但是,它们不向下兼容。 The Rails 2 app will not work with Rails 4.1. Rails 2应用程序不适用于Rails 4.1。 What if you are asked to urgently debug the Rails 2 app while hacking on Rails 4 one? 如果在骇入Rails 4 one时被要求紧急调试Rails 2应用怎么办? Uninstall global Rails, install Rails 2, make the hack, then uninstall Rails 2 and reinstall the new one again, just so you can run the tests for your one-line bug fix? 卸载全局Rails,安装Rails 2,进行破解,然后卸载Rails 2并再次重新安装新的Rails,只是为了您可以运行测试以解决一站式错误?

That's where gemsets come in. You have environment per-application so that each application can be run self-sufficiently, with no versioning conflicts. 这就是使用gemset的地方。每个应用程序都有环境,因此每个应用程序都可以自足运行,而不会发生版本冲突。

If you don't envision such scenarios of version conflict on your machine (ie if you can only imagine working on one project), gemsets are completely irrelevant. 如果您不希望在计算机上出现这种版本冲突的情况(即,如果您只能想象在一个项目上工作),那么gemset是完全不相关的。

EDIT after some confusion still in comments :) Let's go step by step, see what happens exactly. 经过一番迷惑之后仍在编辑中:)让我们一步一步走,看看到底发生了什么。

$ mkdir myapp
$ cd myapp

You're now in an empty directory. 您现在位于空目录中。

$ rvm use ruby-2.1.3@myapp --ruby-version --create

rvm creates a new gemset, named ruby-2.1.3@myapp , which will be run with Ruby 2.1.3. rvm创建一个新的宝石集,名为ruby-2.1.3@myapp ,它将与Ruby 2.1.3一起运行。 As a consequence, you have a new directory at ~/.rvm/gems/ruby-2.1.3@myapp , where your gemset will be. 因此,您将在~/.rvm/gems/ruby-2.1.3@myapp一个新目录,该目录将位于您的gemset。 You also have two new files in your previously empty myapp directory: .ruby-version (which contains a single line saying ruby-2.1.3 ) and .ruby-version (containing the line myapp ). 在先前为空的myapp目录中,您还有两个新文件: .ruby-version (包含一行,表示ruby-2.1.3 )和.ruby-version (包含行myapp )。 These two lines are read by rvm every time you enter the myapp directory: it sets the current Ruby and gemset for you. 每次您进入myapp目录时, rvm都会读取这两行:它为您设置了当前的Ruby和gemset。

$ gem install rails

Having recognised that the current gemset is now ruby-2.1.3@myapp , the gem install command will download the newest rails gem, as well as all its dependencies, and put them in your gemset directory ( ~/.rvm/gems/ruby-2.1.3@myapp/ ). 认识到当前的gemset现在是ruby-2.1.3@myappgem install命令将下载最新的rails gem及其所有依赖项,并将它们放在您的gemset目录中( ~/.rvm/gems/ruby-2.1.3@myapp/ )。

$ get install rails --version=4.0.8

If you try this, it will dutifully install Rails 4.0.8, but since you have a newer version in your gemset and your application is not specifically having any requirements, the newer one will take precedence. 如果您尝试这种方法,它将忠实地安装Rails 4.0.8,但是由于您的gemset中有较新的版本,并且您的应用程序没有特别的要求,因此以新版本为准。 This is normally not what you want; 这通常不是您想要的; and anyway, there is rarely a reason to develop a project to comply with two different versions of a library (unless you're developing a library or a plugin; that's a different story). 而且无论如何,很少有理由开发一个项目以符合两个不同版本的库(除非您要开发一个库或一个插件;这是另外一回事)。

$ rails new .

rails is actually executing ~/.rvm/gems/ruby-2.1.3@myapp/bin/rails . rails实际上正在执行~/.rvm/gems/ruby-2.1.3@myapp/bin/rails If you were not in myapp directory, linked to the gemset, this command would have failed (if you didn't have Rails installed in the global environment), or executed globally installed Rails (if you did). 如果您不在myapp目录中(链接到gemset),则该命令将失败(如果未在全局环境中安装Rails),或者将执行已全局安装的Rails(如果已安装)。

So, it's not really designed to have two versions of Rails simultaneously in the same project. 因此,实际上并不是在同一项目中同时具有两个版本的Rails。 But when you make another project, with another gemset (say, ruby-2.1.3@myotherapp ), you could have a different version of Rails while you're there. 但是,当您使用另一个gemset创建另一个项目(例如ruby-2.1.3@myotherapp )时,在那里可能会使用其他版本的Rails。 The version automagically changes just based on which directory you cd into. 该版本自动的改变只是根据你的目录上cd到。

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

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