Type:
Class

ARGF is a stream designed for use in scripts that process files given as command-line arguments or passed in via STDIN.

The arguments passed to your script are stored in the ARGV Array, one argument per element. ARGF assumes that any arguments that aren't filenames have been removed from ARGV. For example:

$ ruby argf.rb --verbose file1 file2

ARGV  #=> ["--verbose", "file1", "file2"]
option = ARGV.shift #=> "--verbose"
ARGV  #=> ["file1", "file2"]

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 Array
ARGV           #=> []
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"
seek

ARGF.seek(amount, whence=IO::SEEK_SET) â 0 Instance Public methods Seeks

2015-03-30 13:27:40
gets

ARGF.gets(sep=$/) â stringARGF.gets(limit) â stringARGF.gets(sep, limit) â string Instance

2015-03-30 11:32:37
to_a

ARGF.to_a(sep=$/) â arrayARGF.to_a(limit) â arrayARGF.to_a(sep, limit) â array Instance Public

2015-03-30 13:35:32
read

ARGF.read([length [, outbuf]]) â string, outbuf, or nil Instance Public methods

2015-03-30 12:44:57
bytes

bytes() Instance Public methods This is a deprecated alias for each_byte

2015-03-30 10:29:26
lineno=

ARGF.lineno = integer â integer Instance Public methods Sets the line number

2015-03-30 11:59:15
readline

ARGF.readline(sep=$/) â stringARGF.readline(limit) â stringARGF.readline(sep, limit) â string

2015-03-30 13:01:06
binmode

ARGF.binmode â ARGF Instance Public methods Puts ARGF into binary

2015-03-30 10:16:13
read_nonblock

ARGF.read_nonblock(maxlen) â stringARGF.read_nonblock(maxlen, outbuf) â outbuf Instance

2015-03-30 12:47:43
lines

lines(*args) Instance Public methods This is a deprecated alias for each_line

2015-03-30 12:05:42