You can now use ARGF to work with a concatenation of each of
these named files. For instance, ARGF.read will return the
contents of file1 followed by the contents of file2.

After a file in ARGV has been read ARGF removes
it from the Array. Thus, after all files have been
read ARGV will be empty.

You can manipulate ARGV yourself to control what
ARGF operates on. If you remove a file from ARGV,
it is ignored by ARGF; if you add files to ARGV,
they are treated as if they were named on the command line. For example:

ARGV.replace ["file1"]
ARGF.readlines# Returns the contents of file1 as an ArrayARGV#=> []ARGV.replace ["file2", "file3"]
ARGF.read# Returns the contents of file2 and file3

If ARGV is empty, ARGF acts as if it contained
STDIN, i.e. the data piped to your script. For example:

$ echo "glark" | ruby -e 'p ARGF.read'
"glark\n"

Public Instance Methods

argv → ARGVclick to toggle source

Returns the ARGV array, which contains the arguments passed to
your script, one per element.

For example:

$ ruby argf.rb -v glark.txt
ARGF.argv #=> ["-v", "glark.txt"]

static VALUE
argf_argv(VALUE argf)
{
return ARGF.argv;
}

binmode → ARGFclick to toggle source

Puts ARGF into binary mode. Once a stream is in binary mode,
it cannot be reset to non-binary mode. This option has the following
effects:

Returns an enumerator which iterates over each line (separated by
sep, which defaults to your platform’s newline character) of each
file in ARGV. If a block is supplied, each line in turn will
be yielded to the block, otherwise an enumerator is returned. The optional
limit argument is a Fixnum specifying the maximum
length of each line; longer lines will be split according to this limit.

This method allows you to treat the files supplied on the command line as a
single file consisting of the concatenation of each named file. After the
last line of the first file has been returned, the first line of the second
file is returned. The ARGF.filename and
ARGF.lineno methods can be used to determine the filename and
line number, respectively, of the current line.

For example, the following code prints out each line of each named file
prefixed with its line number, displaying the filename once per file:

Iterates over each byte of each file in +ARGV+.
A byte is returned as a +Fixnum+ in the range 0..255.
This method allows you to treat the files supplied on the command line as
a single file consisting of the concatenation of each named file. After
the last byte of the first file has been returned, the first byte of the
second file is returned. The +ARGF.filename+ method can be used to
determine the filename of the current byte.
If no block is given, an enumerator is returned instead.

This method allows you to treat the files supplied on the command line as a
single file consisting of the concatenation of each named file. After the
last character of the first file has been returned, the first character of
the second file is returned. The ARGF.filename method can be
used to determine the name of the file in which the current character
appears.

This method allows you to treat the files supplied on the command line as a
single file consisting of the concatenation of each named file. After the
last codepoint of the first file has been returned, the first codepoint of
the second file is returned. The ARGF.filename method can be
used to determine the name of the file in which the current codepoint
appears.

Returns an enumerator which iterates over each line (separated by
sep, which defaults to your platform’s newline character) of each
file in ARGV. If a block is supplied, each line in turn will
be yielded to the block, otherwise an enumerator is returned. The optional
limit argument is a Fixnum specifying the maximum
length of each line; longer lines will be split according to this limit.

This method allows you to treat the files supplied on the command line as a
single file consisting of the concatenation of each named file. After the
last line of the first file has been returned, the first line of the second
file is returned. The ARGF.filename and
ARGF.lineno methods can be used to determine the filename and
line number, respectively, of the current line.

For example, the following code prints out each line of each named file
prefixed with its line number, displaying the filename once per file:

Returns the external encoding for files read from +ARGF+ as an +Encoding+
object. The external encoding is the encoding of the text as stored in a
file. Contrast with +ARGF.internal_encoding+, which is the encoding used
to represent this text within Ruby.
To set the external encoding use +ARGF.set_encoding+.

Reads the next character from ARGF and returns it as a
String. Returns nil at the end of the stream.

ARGF treats the files named on the command line as a single
file created by concatenating their contents. After returning the last
character of the first file, it returns the first character of the second
file, and so on.

Sets the filename extension for inplace editing mode to the given String.
Each file being edited has this value appended to its filename. The
modified file is saved under this new name.
For example:
$ ruby argf.rb file.txt
ARGF.inplace_mode = '.bak'
ARGF.lines do |line|
print line.sub("foo","bar")
end

Each line of file.txt has the first occurrence of “foo” replaced
with “bar”, then the new line is written out to file.txt.bak.

Returns the internal encoding for strings read from ARGF as an
Encoding object.

If ARGF.set_encoding has been called with two encoding names,
the second is returned. Otherwise, if
Encoding.default_external has been set, that value is
returned. Failing that, if a default external encoding was specified on the
command-line, that value is used. If the encoding is unknown, nil is
returned.

Writes the given object(s) to ios. The stream must be opened for
writing. If the output field separator ($,) is not
nil, it will be inserted between each object. If the output
record separator ($\) is not nil, it will be
appended to the output. If no arguments are given, prints $_.
Objects that aren’t strings will be converted by calling their
to_s method. With no argument, prints the contents of the
variable $_. Returns nil.

If obj is Numeric, write the character whose code is
the least-significant byte of obj, otherwise write the first byte
of the string representation of obj to ios. Note: This
method is not safe for use with multi-byte characters as it will truncate
them.

Writes the given objects to ios as with IO#print.
Writes a record separator (typically a newline) after any that do not
already end with a newline sequence. If called with an array argument,
writes each element on a new line. If called without arguments, outputs a
single record separator.

Reads _length_ bytes from ARGF. The files named on the command line
are concatenated and treated as a single file by this method, so when
called without arguments the contents of this pseudo file are returned in
their entirety.
_length_ must be a non-negative integer or nil. If it is a positive
integer, +read+ tries to read at most _length_ bytes. It returns nil
if an EOF was encountered before anything could be read. Fewer than
_length_ bytes may be returned if an EOF is encountered during the read.
If _length_ is omitted or is _nil_, it reads until EOF. A String is
returned even if EOF is encountered before any data is read.
If _length_ is zero, it returns _""_.
If the optional _outbuf_ argument is present, it must reference a String,
which will receive the data.
The <i>outbuf</i> will contain only the received data after the method call
even if it is not empty at the beginning.

Reads at most maxlen bytes from the ARGF
stream. It blocks only if ARGF has no data immediately
available. If the optional outbuf argument is present, it must
reference a String, which will receive the data.
The outbuf will contain only the received data after the method
call even if it is not empty at the beginning. It raises
EOFError on end of file.

readpartial is designed for streams such as pipes, sockets,
and ttys. It blocks only when no data is immediately available. This means
that it blocks only when following all conditions hold:

The byte buffer in the IO object is empty.

The content of the stream is empty.

The stream has not reached EOF.

When readpartial blocks, it waits for data or EOF. If some
data is read, readpartial returns with the data. If EOF is
reached, readpartial raises an EOFError.

When readpartial doesn’t block, it returns or raises
immediately. If the byte buffer is not empty, it returns the data in the
buffer. Otherwise, if the stream has some content, it returns the data in
the stream. If the stream reaches EOF an EOFError is raised.

If single argument is specified, strings read from ARGF are tagged with the encoding specified.

If two encoding names separated by a colon are given, e.g. “ascii:utf-8”,
the read string is converted from the first encoding (external encoding) to
the second encoding (internal encoding), then tagged with the second
encoding.

If two arguments are specified, they must be encoding objects or encoding
names. Again, the first specifies the external encoding; the second
specifies the internal encoding.

If the external encoding and the internal encoding are specified, the
optional Hash argument can be used to adjust the conversion
process. The structure of this hash is explained in the +String#encode+
documentation.

For example:

ARGF.set_encoding('ascii') # Tag the input as US-ASCII textARGF.set_encoding(Encoding::UTF_8) # Tag the input as UTF-8 textARGF.set_encoding('utf-8','ascii') # Transcode the input from US-ASCII# to UTF-8.