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"
readlines
  • References/Ruby on Rails/Ruby/Classes/ARGF

ARGF.readlines(sep=$/) â arrayARGF.readlines(limit) â arrayARGF.readlines(sep, limit) â array

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

ARGF.binmode? â true or false Instance Public methods

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

ARGF.eof? â true or false Instance Public methods Returns true if the current

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

ARGF.close â ARGF Instance Public methods Closes

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

ARGF.inplace_mode = ext â ARGF Instance Public methods

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

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

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

ARGF.eof? â true or falseARGF.eof â true or false Instance Public methods Returns

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

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

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

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

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

ARGF.filename â String Instance Public methods Returns the current filename

2025-01-10 15:47:30