Ruby/Development/ARGV

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Assign value to ARGV

#!/usr/bin/env ruby
ARGV << "myFile.txt"
print while gets



Get command line input through ARGV

n = ARGV.size
argstr = """ + ARGV*"," + """
puts "#{n} arguments..."
puts "They are: #{argstr}"
puts "Note that ARGV[0] = #{ARGV[0]}"



Ruby places parameters that are appended to the command line into a special array called ARGV.

puts ARGV.join("-")
# From the command prompt, run the script like so:
ruby argv.rb
# The result will be blank, but then try to run it like so:
ruby argv.rb test 123



The two filenames are both command line arguments

#!/usr/bin/env ruby
from_filename = ARGV[0]
destination_filename = ARGV[1]