ARGF
ARGF
is a module in Ruby used for reading files passed as command line arguments or in standard input (STDIN
) in a terminal. A Ruby script using ARGF
may be thought of as implementing a subset of the functions of the Unix command cat
.
cat file1# Output: File 1 contentscat file2# Output: File2 contentsecho "standard input" | cat# Output: standard input
The cat
command can accept a filename or it can pipe (|
) another command as standard input.
ARGF
- The F Is for Files
# argf.rbputs ARGF.read
The ARGF
module has a method .read
that can be used to read the entire contents of a file(s) passed in as a command line argument(s).
ruby argf.rb file1 file2# Output:# File 1 contents# File 2 contents
If no file is passed in, ARGF
can also detect the standard input stream being piped in.
echo "STDIN" | ruby argf.rb# Output:# STDIN
A combination of files and standard input is accepted too.
echo "STDIN" | ruby argf.rb file1 file2 -# Output:# File 1 contents# File 2 contents# STDIN
ARGV
- The V Is for Variable
Under the hood, there is a Ruby array termed ARGV
which contains the names of all arguments passed to the file at the command line or through STDIN
.
# argf_inspect_argv.rbputs ARGV.inspect
The ARGV
array disregards whether the arguments are valid file names or not.
ruby argf_inspect_argv.rb file1 file2# Output: ["file1", "file2"]ruby argf_inspect_argv.rb one two# Output: ["one", "two"]
.shift
As is often the case with terminal commands, flags like --verbose
or --all
are often used.
ruby argf_inspect_argv.rb --all one two# Output: ["--all", "one", "two"]
ARGV
stores the names of the files and other arguments passed in through the command line.
The .shift
method of ARGV can be used to remove any arguments from the ARGV array that are not files.
# argv_shift.rbARGV.shiftputs ARGV.inspect
ruby argv_shift.rb --all one two["one", "two"]
Note: Files that are processed by
ARGF.read
are shifted off theARGV
array.
.readlines
The method .readlines
creates an array of strings. Elements within the array represent the file contents passed to the Ruby script. It’s commonly used for separating the lines of a file into array items so they can be iterated over with other methods such as .each
.
# argv_readlines.rblines = ARGF.readlinesputs linesputs lines.inspect
ruby argv_readlines.rb file1 file2# Output:# Contents of file1# Contents of file2# ["Contents of file1\n", "Contents of file2\n"]
.readlines
will not accept command line arguments that are not valid file names.
ruby argv_readlines.rb one two=> argv_readlines.rb:1:in `readlines': No such file or directory @ rb_sysopen - one (Errno::ENOENT) from argv_readlines.rb:1:in `<main>'
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Ruby on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn Ruby
Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.Beginner Friendly9 hours