Omar Khan Omar Khan - 1 month ago 6
Ruby Question

Why ARGV.shift in rails startup script

The start-up script for rails has the following code:

require 'rubygems'

version = ">= 0"

if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end

gem 'railties', version
load Gem.bin_path('railties', 'rails', version)


Why are we doing
ARGV.shift
? Is it just a canonical way of dealing with ARGV after we have consumed an argument or do we need to do it for some reason?

Answer

Most likely this is because Rails uses ARGV to read its own command line arguments. If the start-up script failed to remove its argument (the first one) from ARGV before starting Rails, Rails would complain about an unexpected first argument, and probably abort.