简体   繁体   中英

clarification on rvm and switching between rails

following http://railsapps.github.io/installrubyonrails-mac.html , I encounter the following commands

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

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? What does that even mean?

And this is the instructions on how to create a new rails project

$ 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? The text says it's to "create a project-specific gemset", but I have no idea what that means. Wouldn't this just install rails 4.1.6 (newest version) ? why not just install rails 4.1.6 globally in the first place then?

Imagine you are a Rails developer in a company that has been doing Rails apps for the last 4 years. You have apps on Rails 2, Rails 3, Rails 4 - as new versions come out, you upgrade your toolset because, why not? Each new version is better.

However, they are not downward compatible. The Rails 2 app will not work with Rails 4.1. What if you are asked to urgently debug the Rails 2 app while hacking on Rails 4 one? 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?

That's where gemsets come in. You have environment per-application so that each application can be run self-sufficiently, with no versioning conflicts.

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.

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. As a consequence, you have a new directory at ~/.rvm/gems/ruby-2.1.3@myapp , where your gemset will be. 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 ). These two lines are read by rvm every time you enter the myapp directory: it sets the current Ruby and gemset for you.

$ 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/ ).

$ 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. 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 . 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).

So, it's not really designed to have two versions of Rails simultaneously in the same project. 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. The version automagically changes just based on which directory you cd into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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