Ruby I/O
Parsing a File
To extract the contents of a file as a single string:
lines = open('test.txt').read
To extract the contents of a file as an array of lines:
lines = open('test.txt').readlines
To process a file line-by-line:
open('test.txt').each do |line|
do something with line
end
Write to a file
output = open('output.txt', 'w')
output.puts("Here is the first line")
output.puts("and here is the second line")
List a directory
list_of_files = Dir.foreach('/some/directory/path')
File Attributes
File.exists?('/some/directory/path')
File.size('/some/directory/path')
more here: http://www.ruby-doc.org/core/classes/File.html
Command-line arguments
ARGV[0]
is the first of the command-line parameters, numbering naturally from zero; it is not the file or script name preceding the parameters, like argv[0]
in C.