Command-line Arguments in Ruby on rails

By: Brian Marick Emailed: 1729 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Command-line arguments are available as strings in the array named ARGV. For example, consider this command line:

prompt> ruby differences.rb old-inventory.txt new-inventory.txt

Within differences.rb, ARGV[0] will name (that is, provide a way to refer to) the string "old-inventory.txt", and ARGV[1] will name "new-inventory.txt".

Instead of “hard-coding” the two files’ names into the script, we can require the user to give them on the command line. The code to do that would look like this:

old_inventory = File.open(ARGV[0]).readlines

new_inventory = File.open(ARGV[1]).readlines

If you give no arguments to a script, ARGV still names an array, but it’s empty. You can see that using irb. irb is just a Ruby script, and you start it without any arguments, so. . .

prompt> irb

irb(main):001:0> ARGV

=> []

irb(main):002:0>

Since there’s nothing in the array, every array index is out of bounds and will return nil:1

irb(main):003:0> ARGV[0]

=> nil

irb(main):004:0> ARGV[1]

=> nil

Because ARGV[0] is nil, the script has a problem when it hits this line: 

old_inventory = File.open(ARGV[0]).readlines

Ruby’s unless construct begins with unless and ends with end. The body body is the text between the two. Unless the expression following the unless is true, the body is executed. Otherwise, it’s skipped. In this case, the body is executed unless the number of elements in the array is equal to 2 (meaning that both arguments were given).2

The body of an unless is just like any other Ruby code; it executes one line at a time. In this case, it prints a message. Then it uses the exit message to stop the script; otherwise, Ruby would continue to try (and fail) to open a file. You don’t have to indent the body, but it makes the code easier to read. I usually indent two spaces.


Ruby Home | All Ruby Tutorials | Latest Ruby Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Brian Marick
The Question Mark Operator in Ruby on rails
unless in Ruby on rails
if . . . elsif . . . else in Ruby on rails
Methods in Ruby on rails
Command-line Arguments in Ruby on rails
Naming conventions in Ruby on rails

More Tutorials in Ruby
Standard Library Packages in Ruby
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
bmbm(width = 0) {|job| ...} in Ruby
CGI class in Ruby
URL encode a string in Ruby
escapeElement() in Ruby
escapeHTML() in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
pretty() in Ruby
Format a Time object as a String in Ruby

More Latest News
Most Viewed Articles (in Ruby )
Command-line Arguments in Ruby on rails
Getting started with Ruby on rails
unless in Ruby on rails
escapeElement() in Ruby
parse(query) in Ruby
Format a Time object as a String in Ruby
dRuby client/server mode sample program
FTP using Ruby
IMAP in Ruby
Using Generator in Ruby
Action Pack - Ruby On rails from request to response
Prompts, Command Lines, Prompts, and irb in Ruby on rails
Naming conventions in Ruby on rails
Methods in Ruby on rails
if . . . elsif . . . else in Ruby on rails
Most Emailed Articles (in Ruby)
Methods in Ruby on rails
Prompts, Command Lines, Prompts, and irb in Ruby on rails
Command-line Arguments in Ruby on rails
Standard Library Packages in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
escapeHTML() in Ruby
unescapeElement() in Ruby
Using Proxy to connect to URLs in Ruby
IMAP in Ruby
Reading emails using POP3 in Ruby
Using Observer in Ruby
Using Forwardable in Ruby
Sending emails and Receiving emails using Ruby On rails
Getting started with Ruby on rails
Naming conventions in Ruby on rails