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:

1
2
3
4
5
$ 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:

1
2
3
4
5
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:

1
2
$ echo "glark" | ruby -e 'p ARGF.read'
"glark\n"
lineno
  • References/Ruby on Rails/Ruby/Classes/ARGF

ARGF.lineno â integer Instance Public methods Returns the current line number

2025-01-10 15:47:30
inspect
  • References/Ruby on Rails/Ruby/Classes/ARGF

inspect() Instance Public methods Alias for:

2025-01-10 15:47:30
to_s
  • References/Ruby on Rails/Ruby/Classes/ARGF

ARGF.to_s â String Instance Public methods Returns âARGFâ.

2025-01-10 15:47:30
each_byte
  • References/Ruby on Rails/Ruby/Classes/ARGF

ARGF.bytes {|byte| block } â ARGFARGF.bytes â an_enumeratorARGF.each_byte {|byte| block } â ARGFARGF.each_byte

2025-01-10 15:47:30
skip
  • References/Ruby on Rails/Ruby/Classes/ARGF

ARGF.skip â ARGF Instance Public methods Sets

2025-01-10 15:47:30
getc
  • References/Ruby on Rails/Ruby/Classes/ARGF

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

2025-01-10 15:47:30
print
  • References/Ruby on Rails/Ruby/Classes/ARGF

ios.print() â nilios.print(obj, ...) â nil Instance Public methods

2025-01-10 15:47:30
file
  • References/Ruby on Rails/Ruby/Classes/ARGF

ARGF.file â IO or File object Instance Public methods Returns the current

2025-01-10 15:47:30
putc
  • References/Ruby on Rails/Ruby/Classes/ARGF

ios.putc(obj) â obj Instance Public methods If obj is Numeric

2025-01-10 15:47:30
external_encoding
  • References/Ruby on Rails/Ruby/Classes/ARGF

ARGF.external_encoding â encoding Instance Public methods

2025-01-10 15:47:30