简体   繁体   中英

Generating a new sample app in rails tutorial

When following along with the rails tutorial, does anyone know what the 5.1.2 means in terms of testing environments? Is there a significance of specifying which rails version to use I'm used to not specifying.

Listing 3.1: Generating a new sample app.

$ cd ~/environment
$ rails _5.1.2_ new sample_app
$ cd sample_app/

https://www.railstutorial.org/book/static_pages

Does anyone have more documentation as to what the underscore means?

I'm not seeing it in this the guides.

http://guides.rubyonrails.org/command_line.html

rails is an executable distributed in a Ruby gem. You get the rails command when you run gem install rails .

A confusing aspect of gems is that you can have multiple versions installed. For example, on my computer I have 3 versions of rails :

$ gem list -e rails

*** LOCAL GEMS ***

rails (5.1.4, 5.0.6, 4.2.10)

When you run a rails command, which version is actually executed? In practice, Ruby always executes the newest version that you have installed. But what if you don't want to use the newest (or don't remember which version is newest)?

That is where the underscore syntax comes in. This is a feature of rubygems lets you specify the gem version you want to execute by including the version surrounded by underscores as the first argument. For example, this runs rails new with rails version 5.0.6:

$ rails _5.0.6_ new

The tutorial you referenced was probably written with a specific version of Rails in mind. So the author is telling you to use the underscore syntax to ensure that you are using the version of Rails they intended, just in case you may have a newer version of Rails installed.

Note that this only matters for rails new command. Once you have generated a Rails app, you can use the special bin/rails script whenever you need to run a Rails command. This script is smart enough to know which version of Rails to use based on the Gemfile in your project.

Side note: the underscore behavior of rubygems does not seem to be documented anywhere. You can see the code that implements the behavior here: https://github.com/rubygems/rubygems/blob/55df5dbb5d917809a27a5881ddf3c0b5543b011f/lib/rubygems/installer.rb#L732-L734

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