The Command Line

In case you haven't been exposed to the concept before, most operating systems (including all flavors of Unix, DOS, and Windows) allow strings to be passed into programs when they start running. They are called command-line arguments even though in a windowing environment there might be no visible command line where the user is typing information. We have been using command-line arguments in a very narrow way up to now, as a list of input files. The following script reads all the files whose names are on the command line, one at a time, and prints their contents.

# Save as cat.rb
while (line = gets)
  puts line
end

Do you remember how that works? gets, without a specified receiver, tries to open the filenames that are given to ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.