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"
closed?

ARGF.closed? â true or false Instance Public methods Returns true

2015-03-30 10:47:25
pos=

ARGF.pos = position â Integer Instance Public methods Seeks to the position

2015-03-30 12:20:16
rewind

ARGF.rewind â 0 Instance Public methods Positions the current file to the

2015-03-30 13:21:02
to_i

ARGF.to_i â fixnum Instance Public methods Returns an integer representing

2015-03-30 13:37:52
write

ARGF.write(string) â integer Instance Public methods Writes string

2015-03-30 13:52:46
to_io

ARGF.to_io â IO Instance Public methods Returns an IO object

2015-03-30 13:43:06
each_char

ARGF.each_char {|char| block } â ARGFARGF.each_char â an_enumerator Instance Public methods

2015-03-30 11:02:13
argv

ARGF.argv â ARGV Instance Public methods Returns the ARGV array

2015-03-30 10:11:54
readchar

ARGF.readchar â String or nil Instance Public methods Reads the next character

2015-03-30 12:58:24
inplace_mode

ARGF.inplace_mode â String Instance Public methods Returns the file extension

2015-03-30 11:38:50