Skip to content

Instantly share code, notes, and snippets.

@kennx

kennx/input Secret

Last active December 14, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennx/a72a5e70fe469f5a9cfc to your computer and use it in GitHub Desktop.
Save kennx/a72a5e70fe469f5a9cfc to your computer and use it in GitHub Desktop.
[22] pry(main)> gets = <<'EOF'
[22] pry(main)* ARGF
[22] pry(main)*
[22] pry(main)* ARGF is a stream designed for use in scripts that process files given as command-line arguments or passed in via STDIN.
[22] pry(main)*
[22] pry(main)* 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:
[22] pry(main)*
[22] pry(main)* $ ruby argf.rb --verbose file1 file2
Error: Couldn't locate a definition for ruby argf.rb --verbose file1 file2!
[22] pry(main)*
[22] pry(main)* ARGV #=> ["--verbose", "file1", "file2"]
[22] pry(main)* option = ARGV.shift #=> "--verbose"
[22] pry(main)* ARGV #=> ["file1", "file2"]
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* 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:
[22] pry(main)*
[22] pry(main)* ARGV.replace ["file1"]
[22] pry(main)* ARGF.readlines # Returns the contents of file1 as an Array
[22] pry(main)* ARGV #=> []
[22] pry(main)* ARGV.replace ["file2", "file3"]
[22] pry(main)* ARGF.read # Returns the contents of file2 and file3
[22] pry(main)* If ARGV is empty, ARGF acts as if it contained STDIN, i.e. the data piped to your script. For example:
[22] pry(main)*
[22] pry(main)* $ echo "glark" | ruby -e 'p ARGF.read'
Error: Couldn't locate a definition for echo "glark" | ruby -e 'p ARGF.read'!
[22] pry(main)* "glark\n"
[22] pry(main)* Public Instance Methods
[22] pry(main)*
[22] pry(main)* argv → ARGV
[22] pry(main)* Returns the ARGV array, which contains the arguments passed to your script, one per element.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ ruby argf.rb -v glark.txt
Error: Couldn't locate a definition for ruby argf.rb -v glark.txt!
[22] pry(main)*
[22] pry(main)* ARGF.argv #=> ["-v", "glark.txt"]
[22] pry(main)* binmode → ARGF
[22] pry(main)* 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:
[22] pry(main)*
F
bytes → an_enumerator
each_byte {|byte| block } → ARGF
each_byte → an_enumerator
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.
For example:
ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
chars {|char| block } → ARGF
chars → an_enumerator
each_char {|char| block } → ARGF
each_char → an_enumerator
Iterates over each character of each file in ARGF.
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 ret[22] pr[22] pry(main)* Newline conversion is disabled.
[22] pry(main)*
[22] pry(main)* Encoding conversion is disabled.
[22] pry(main)*
[22] pry(main)* Content is treated as ASCII-8BIT.
[22] pry(main)*
[22] pry(main)* binmode? → true or false
[22] pry(main)* Returns true if +ARGF+ is being read in binary mode; false otherwise. (To
[22] pry(main)* enable binary mode use +ARGF.binmode+.
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.binmode? #=> false
[22] pry(main)* ARGF.binmode
[22] pry(main)* ARGF.binmode? #=> true
[22] pry(main)* bytes {|byte| block } → ARGF
[22] pry(main)* bytes → an_enumerator
[22] pry(main)* each_byte {|byte| block } → ARGF
[22] pry(main)* each_byte → an_enumerator
[22] pry(main)* Iterates over each byte of each file in +ARGV+.
[22] pry(main)* A byte is returned as a +Fixnum+ in the range 0..255.
[22] pry(main)*
[22] pry(main)* This method allows you to treat the files supplied on the command line as
[22] pry(main)* a single file consisting of the concatenation of each named file. After
[22] pry(main)* the last byte of the first file has been returned, the first byte of the
[22] pry(main)* second file is returned. The +ARGF.filename+ method can be used to
[22] pry(main)* determine the filename of the current byte.
[22] pry(main)*
[22] pry(main)* If no block is given, an enumerator is returned instead.
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
[22] pry(main)* chars {|char| block } → ARGF
[22] pry(main)* chars → an_enumerator
[22] pry(main)* each_char {|char| block } → ARGF
[22] pry(main)* each_char → an_enumerator
[22] pry(main)* Iterates over each character of each file in ARGF.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* If no block is given, an enumerator is returned instead.
[22] pry(main)*
[22] pry(main)* close → ARGF
[22] pry(main)* Closes the current file and skips to the next in the stream. Trying to
[22] pry(main)* close a file that has already been closed causes an +IOError+ to be
[22] pry(main)* raised.
[22] pry(main)* For example:
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.
If no block is given, an enumerator is returned instead.
each(sep=$/) {|line| block } → ARGF
each(sep=$/,limit) {|line| block } → ARGF
each(...) → an_enumerator
each_line(sep=$/) {|line| block } → ARGF
each_line(sep=$/,limit) {|line| block } → ARGF
each_line(...) → an_enumerator
lines(sep=$/) {|line| block } → ARGF
lines(sep=$/,limit) {|line| block } → ARGF
lines(...) → an_enumerator
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 yielde[22] pry(main)* 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.
[22] pry(main)* $ ruby argf.rb foo bar
Error: Couldn't locate a definition for ruby argf.rb foo bar!
[22] pry(main)*
[22] pry(main)* ARGF.filename #=> "foo"
[22] pry(main)* ARGF.close
[22] pry(main)* ARGF.filename #=> "bar"
[22] pry(main)* ARGF.close
[22] pry(main)* ARGF.close #=> closed stream (IOError)
[22] pry(main)* closed? → true or false
[22] pry(main)* Returns true if the current file has been closed; false otherwise. Use ARGF.close to actually close the current file.
[22] pry(main)*
[22] pry(main)* codepoints {|codepoint| block } → ARGF
[22] pry(main)* codepoints → an_enumerator
[22] pry(main)* each_codepoint {|codepoint| block } → ARGF
[22] pry(main)* each_codepoint → an_enumerator
[22] pry(main)* Iterates over each codepoint of each file in ARGF.
[22] pry(main)*
[22] pry(main)* 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.
d 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:
ARGF.lines do |line|
puts ARGF.filename if ARGF.lineno == 1
puts "#{ARGF.lineno}: #{line}"
end
bytes {|byte| block } → ARGF
bytes → an_enumerator
each_byte {|byte| block } → ARGF
each_byte → an_enumerator
Iterates over each byte of each file in +ARGV+.
A byte is returned as a +Fixnum+ in the range 0..255.
[22] pry(main)* 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.
[22] pry(main)* the first file
[22] pry(main)* If no block is given, an enumerator is returned instead.
[22] pry(main)*
[22] pry(main)* each(sep=$/) {|line| block } → ARGF
[22] pry(main)* each(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* each(...) → an_enumerator
[22] pry(main)* each_line(sep=$/) {|line| block } → ARGF
[22] pry(main)* each_line(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* each_line(...) → an_enumerator
[22] pry(main)* lines(sep=$/) {|line| block } → ARGF
[22] pry(main)* lines(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* lines(...) → an_enumerator
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* For example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:
ned, 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.
For example:
ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
chars {|char| block } → ARGF
chars → an_enumerator
each_char {|char| block } → ARGF
each_char → an_enumerator
Iterates over each character of each file in ARGF.
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.
If no block is given, an enumerator is returned instead.
codepoints {|codepoint| block } → ARGF
codepoints → an_enumerator
each_codepoint {|codepoint| block } → ARGF
[22] pry(main)* For example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:
[22] pry(main)*
[22] pry(main)* ARGF.lines do |line|
[22] pry(main)* puts ARGF.filename if ARGF.lineno == 1
[22] pry(main)* puts "#{ARGF.lineno}: #{line}"
[22] pry(main)* end
[22] pry(main)* bytes {|byte| block } → ARGF
[22] pry(main)* bytes → an_enumerator
[22] pry(main)* each_byte {|byte| block } → ARGF
[22] pry(main)* each_byte → an_enumerator
[22] pry(main)* Iterates over each byte of each file in +ARGV+.
[22] pry(main)* A byte is returned as a +Fixnum+ in the range 0..255.
[22] pry(main)*
[22] pry(main)* This method allows you to treat the files supplied on the command line as
[22] pry(main)* a single file consisting of the concatenation of each named file. After
[22] pry(main)* the last byte of the first file has been returned, the first byte of the
of each file in ARGF.
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.
If no block is given, an enumerator is returned instead.
each(sep=$/) {|line| block } → ARGF
each(sep=$/,limit) {|line| block } → ARGF
each(...) → an_enumerator
each_line(sep=$/) {|line| block } → ARGF
each_line(sep=$/,limit) {|line| block } → ARGF
each_line(...) → an_enumerator
lines(sep=$/) {|line| block } → ARGF
lines(sep=$/,limit) {|line| block } → ARGF
lines(...) → an_enumerator
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 yielde[22] pry(main)* second file is returned. The +ARGF.filename+ method can be used toe used to
[22] pry(main)* determine the filename of the current byte.
[22] pry(main)*
[22] pry(main)* If no block is given, an enumerator is returned instead.
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
[22] pry(main)* chars {|char| block } → ARGF
[22] pry(main)* chars → an_enumerator
[22] pry(main)* each_char {|char| block } → ARGF
[22] pry(main)* each_char → an_enumerator
[22] pry(main)* Iterates over each character of each file in ARGF.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* If no block is given, an enumerator is returned instead.
[22] pry(main)*
[22] pry(main)* codepoints {|codepoint| block } → ARGF
[22] pry(main)* codepoints → an_enumerator
[22] pry(main)* each_codepoint {|codepoint| block } → ARGF
[22] pry(main)* each_codepoint → an_enumerator
[22] pry(main)* Iterates over each codepoint of each file in ARGF.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* If no block is given, an enumerator is returned instead.
[22] pry(main)*
[22] pry(main)* each(sep=$/) {|line| block } → ARGF
[22] pry(main)* each(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* each(...) → an_enumerator
[22] pry(main)* each_line(sep=$/) {|line| block } → ARGF
[22] pry(main)* each_line(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* each_line(...) → an_enumerator
[22] pry(main)* lines(sep=$/) {|line| block } → ARGF
[22] pry(main)* lines(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* lines(...) → an_enumerator
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* 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.
F.eof? #=> false
3.times { ARGF.readchar }
ARGF.eof? #=> false
ARGF.readchar #=> "\n"
ARGF.eof? #=> true
eof? → true or false
eof → true or false
Returns true if the current file in ARGF is at end of file, i.e. it has no data to read. The stream must be opened for reading or an IOError will be raised.
$ echo "eof" | ruby argf.rb
ARGF.eof? #=> false
3.times { ARGF.readchar }
ARGF.eof? #=> false
ARGF.readchar #=> "\n"
ARGF.eof? #=> true
external_encoding → encoding
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+.
For example:
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* For example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:
[22] pry(main)*
[22] pry(main)* ARGF.lines do |line|
[22] pry(main)* puts ARGF.filename if ARGF.lineno == 1
[22] pry(main)* puts "#{ARGF.lineno}: #{line}"
[22] pry(main)* end
[22] pry(main)* eof? → true or false
[22] pry(main)* eof → true or false
[22] pry(main)* Returns true if the current file in ARGF is at end of file, i.e. it has no data to read. The stream must be opened for reading or an IOError will be raised.
[22] pry(main)*
[22] pry(main)* $ echo "eof" | ruby argf.rb
Error: Couldn't locate a definition for echo "eof" | ruby argf.rb!
[22] pry(main)*
[22] pry(main)* ARGF.eof? #=> false
[22] pry(main)* 3.times { ARGF.readchar }
[22] pry(main)* ARGF.eof? #=> false
[22] pry(main)* ARGF.readchar #=> "\n"
[22] pry(main)* ARGF.eof? #=> true
[22] pry(main)* eof? → true or false
[22] pry(main)* eof → true or false
[22] pry(main)* Returns true if the current file in ARGF is at end of file, i.e. it has no data to read. The stream must be opened for reading or an IOError will be raised.
[22] pry(main)*
[22] pry(main)* $ echo "eof" | ruby argf.rb
Error: Couldn't locate a definition for echo "eof" | ruby argf.rb!
[22] pry(main)*
[22] pry(main)* ARGF.eof? #=> false
[22] pry(main)* 3.times { ARGF.readchar }
[22] pry(main)* ARGF.eof? #=> false
[22] pry(main)* ARGF.readchar #=> "\n"
[22] pry(main)* ARGF.eof? #=> true
[22] pry(main)* external_encoding → encoding
[22] pry(main)* Returns the external encoding for files read from +ARGF+ as an +Encoding+
[22] pry(main)* object. The external encoding is the encoding of the text as stored in a
[22] pry(main)* file. Contrast with +ARGF.internal_encoding+, which is the encoding used
[22] pry(main)* to represent this text within Ruby.
[22] pry(main)*
[22] pry(main)* To set the external encoding use +ARGF.set_encoding+.
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.external_encoding #=> #<Encoding:UTF-8>
[22] pry(main)* file → IO or File object
[22] pry(main)* Returns the current file as an IO or File object. #<IO:<STDIN>> is returned when the current file is STDIN.
[22] pry(main)*
[22] pry(main)* For example:
ARGF.getbyte #=> 10
ARGF.getbyte #=> nil
getc → String or nil
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.
For example:
$ echo "foo" > file
$ ruby argf.rb file
ARGF.getc #=> "f"
ARGF.getc #=> "o"
ARGF.getc #=> "o"
ARGF.getc #=> "\n"
ARGF.getc #=> nil
ARGF.getc #=> nil
gets(sep=$/) → string
gets(limit) → string
gets(sep, limit) → string
Returns the next line from the current file in ARGF.
By default lines are assumed to be separated by +$/+; to use a different character as a separator, supply it as a String for the sep argument.
The optional limit argument specifies how many characters of each line to return. By default all characters are returned.
inplace_mode → String
[22] pry(main)* [22] pry(main)*
[22] pry(main)* $ echo "foo" > foo
Error: Couldn't locate a definition for echo "foo" > foo!
[22] pry(main)* $ echo "bar" > bar
Error: Couldn't locate a definition for echo "bar" > bar!
[22] pry(main)*
[22] pry(main)* $ ruby argf.rb foo bar
Error: Couldn't locate a definition for ruby argf.rb foo bar!
[22] pry(main)*
[22] pry(main)* ARGF.file #=> #<File:foo>
[22] pry(main)* ARGF.read(5) #=> "foo\nb"
[22] pry(main)* ARGF.file #=> #<File:bar>
[22] pry(main)* filename → String
[22] pry(main)* path → String
[22] pry(main)* Returns the current filename. “-” is returned when the current file is STDIN.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ echo "foo" > foo
Error: Couldn't locate a definition for echo "foo" > foo!
[22] pry(main)* $ echo "bar" > bar
Error: Couldn't locate a definition for echo "bar" > bar!
[22] pry(main)* $ echo "glark" > glark
Error: Couldn't locate a definition for echo "glark" > glark!
[22] pry(main)*
[22] pry(main)* $ ruby argf.rb foo bar glark
Error: Couldn't locate a definition for ruby argf.rb foo bar glark!
[22] pry(main)*
[22] pry(main)* ARGF.filename #=> "foo"
[22] pry(main)* ARGF.read(5) #=> "foo\nb"
ARGF.getbyte #=> 10
ARGF.getbyte #=> nil
getc → String or nil
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.
For example:
$ echo "foo" > file
$ ruby argf.rb file
ARGF.getc #=> "f"
ARGF.getc #=> "o"
ARGF.getc #=> "o"
ARGF.getc #=> "\n"
ARGF.getc #=> nil
ARGF.getc #=> nil
gets(sep=$/) → string
gets(limit) → string
gets(sep, limit) → string
Returns the next line from the current file in ARGF.
By default lines are assumed to be separated by +$/+; to use a different character as a separator, supply it as a String for the sep argument.
The optional limit argument specifies how many characters of each line to return. By default all characters are returned.
inplace_mode → String
[22] pry(main)* ARGF.filename #=> "bar"lename #=> "bar"
[22] pry(main)* ARGF.skip
[22] pry(main)* ARGF.filename #=> "glark"
[22] pry(main)* fileno → fixnum
[22] pry(main)* to_i → fixnum
[22] pry(main)* Returns an integer representing the numeric file descriptor for the current file. Raises an ArgumentError if there isn’t a current file.
[22] pry(main)*
[22] pry(main)* ARGF.fileno #=> 3
[22] pry(main)* getbyte → Fixnum or nil
[22] pry(main)* Gets the next 8-bit byte (0..255) from ARGF. Returns nil if called at the end of the stream.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ echo "foo" > file
Error: Couldn't locate a definition for echo "foo" > file!
[22] pry(main)* $ ruby argf.rb file
Error: Couldn't locate a definition for ruby argf.rb file!
[22] pry(main)*
[22] pry(main)* ARGF.getbyte #=> 102
[22] pry(main)* ARGF.getbyte #=> 111
[22] pry(main)* ARGF.getbyte #=> 111
[22] pry(main)* ARGF.getbyte #=> 10
ames of modified files under inplace-edit mode. This value can be set using ARGF.inplace_mode= or passing the -i switch to the Ruby binary.
inplace_mode = ext → ARGF
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.
internal_encoding → encoding
Returns the internal encoding for strings read from ARGF as an Encoding object.
[22] pry(main)* ARGF.getbyte #=> 10d 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 en[22] pry(main)* ARGF.getbyte #=> nilline, that value is u
[22] pry(main)* getc → String or nil
[22] pry(main)* Reads the next character from ARGF and returns it as a String. Returns nil at the end of the stream.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ echo "foo" > file
Error: Couldn't locate a definition for echo "foo" > file!
[22] pry(main)* $ ruby argf.rb file
Error: Couldn't locate a definition for ruby argf.rb file!
[22] pry(main)*
[22] pry(main)* ARGF.getc #=> "f"
[22] pry(main)* ARGF.getc #=> "o"
[22] pry(main)* ARGF.getc #=> "o"
[22] pry(main)* ARGF.getc #=> "\n"
[22] pry(main)* ARGF.getc #=> nil
[22] pry(main)* ARGF.getc #=> nil
[22] pry(main)* gets(sep=$/) → string
[22] pry(main)* gets(limit) → string
[22] pry(main)* gets(sep, limit) → string
[22] pry(main)* Returns the next line from the current file in ARGF.
[22] pry(main)*
[22] pry(main)* By default lines are assumed to be separated by +$/+; to use a different character as a separator, supply it as a String for the sep argument.
ames of modified files under inplace-edit mode. This value can be set using ARGF.inplace_mode= or passing the -i switch to the Ruby binary.
inplace_mode = ext → ARGF
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.
internal_encoding → encoding
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 en[22] pry(main)* fied on the command-line, that value is u[22] pry(main)*
[22] pry(main)* The optional limit argument specifies how many characters of each line to return. By default all characters are returned.
[22] pry(main)*
[22] pry(main)* inplace_mode → String
[22] pry(main)* Returns the file extension appended to the names of modified files under inplace-edit mode. This value can be set using ARGF.inplace_mode= or passing the -i switch to the Ruby binary.
[22] pry(main)*
[22] pry(main)* inplace_mode = ext → ARGF
[22] pry(main)* Sets the filename extension for inplace editing mode to the given String.
[22] pry(main)* Each file being edited has this value appended to its filename. The
[22] pry(main)* modified file is saved under this new name.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ ruby argf.rb file.txt
[22] pry(main)*
[22] pry(main)* ARGF.inplace_mode = '.bak'
[22] pry(main)* ARGF.lines do |line|
[22] pry(main)* print line.sub("foo","bar")
[22] pry(main)* end
[22] pry(main)* 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.
[22] pry(main)*
g is unknown, nil is returned.
lineno → integer
Returns the current line number of ARGF as a whole. This value can be set manually with ARGF.lineno=.
For example:
ARGF.lineno #=> 0
ARGF.readline #=> "This is line 1\n"
ARGF.lineno #=> 1
lineno = number → nil
Sets the line number of ARGF as a whole to the given Integer.
ARGF sets the line number automatically as you read data, so normally you will not need to set it explicitly. To access the current line number use ARGF.lineno.
For example:
ARGF.lineno #=> 0
ARGF.readline #=> "This is line 1\n"
ARGF.lineno #=> 1
ARGF.lineno = 0 #=> nil
ARGF.lineno #=> 0
each(sep=$/) {|line| block } → ARGF
each(sep=$/,limit) {|line| block } → ARGF
each(...) → an_enumerator
each_line(sep=$/) {|line| block } → ARGF
each_line(sep=$/,limit) {|line| block } → ARGF
each_line(...) → an_enumerator
lines(sep=$/) {|line| block } → ARGF
lines(sep=$/,limit) {|line| block } → ARGF
lines(...) → an_enumerator
[22] pry(main)* internal_encoding → encodinging
[22] pry(main)* Returns the internal encoding for strings read from ARGF as an Encoding object.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* lineno → integer
[22] pry(main)* Returns the current line number of ARGF as a whole. This value can be set manually with ARGF.lineno=.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.lineno #=> 0
[22] pry(main)* ARGF.readline #=> "This is line 1\n"
[22] pry(main)* ARGF.lineno #=> 1
[22] pry(main)* lineno = number → nil
[22] pry(main)* Sets the line number of ARGF as a whole to the given Integer.
[22] pry(main)*
[22] pry(main)* ARGF sets the line number automatically as you read data, so normally you will not need to set it explicitly. To access the current line number use ARGF.lineno.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.lineno #=> 0
[22] pry(main)* ARGF.readline #=> "This is line 1\n"
[22] pry(main)* ARGF.lineno #=> 1
ates 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:
ARGF.lines do |line|
puts ARGF.filename if ARGF.lineno == 1
puts "#{ARGF.lineno}: #{line}"
end
[22] pry(main)* ARGF.lineno #=> 1
[22] pry(main)* ARGF.lineno = 0 #=> nil
[22] pry(main)* ARGF.lineno #=> 0
[22] pry(main)* each(sep=$/) {|line| block } → ARGF
[22] pry(main)* each(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* each(...) → an_enumerator
[22] pry(main)* each_line(sep=$/) {|line| block } → ARGF
[22] pry(main)* each_line(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* each_line(...) → an_enumerator
[22] pry(main)* lines(sep=$/) {|line| block } → ARGF
[22] pry(main)* lines(sep=$/,limit) {|line| block } → ARGF
[22] pry(main)* lines(...) → an_enumerator
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* For example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:
[22] pry(main)*
[22] pry(main)* ARGF.lines do |line|
ns the current filename. “-” is returned when the current file is STDIN.
For example:
$ echo "foo" > foo
$ echo "bar" > bar
$ echo "glark" > glark
$ ruby argf.rb foo bar glark
ARGF.filename #=> "foo"
ARGF.read(5) #=> "foo\nb"
ARGF.filename #=> "bar"
ARGF.skip
ARGF.filename #=> "glark"
tell → Integer
pos → Integer
Returns the current offset (in bytes) of the current file in ARGF.
ARGF.pos #=> 0
ARGF.gets #=> "This is line one\n"
ARGF.pos #=> 17
pos = position → Integer
Seeks to the position given by position (in bytes) in ARGF.
For example:
ARGF.pos = 17
ARGF.gets #=> "This is line two\n"
print() → nil
print(obj, ...) → nil
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 ($\</code[22] pry(main)* puts ARGF.filename if ARGF.lineno == 1If no arguments are given, prints $_. Objects that aren’t strings [22] pry(main)* puts ARGF.filename if ARGF.lineno == 1
[22] pry(main)* puts "#{ARGF.lineno}: #{line}"
[22] pry(main)* end
[22] pry(main)* filename → String
[22] pry(main)* path → String
[22] pry(main)* Returns the current filename. “-” is returned when the current file is STDIN.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ echo "foo" > foo
Error: Couldn't locate a definition for echo "foo" > foo!
[22] pry(main)* $ echo "bar" > bar
Error: Couldn't locate a definition for echo "bar" > bar!
[22] pry(main)* $ echo "glark" > glark
Error: Couldn't locate a definition for echo "glark" > glark!
[22] pry(main)*
[22] pry(main)* $ ruby argf.rb foo bar glark
Error: Couldn't locate a definition for ruby argf.rb foo bar glark!
[22] pry(main)*
[22] pry(main)* ARGF.filename #=> "foo"
[22] pry(main)* ARGF.read(5) #=> "foo\nb"
[22] pry(main)* ARGF.filename #=> "bar"
their to_s method. With no argument, prints the contents of the variable $_. Returns nil.
$stdout.print("This is ", 100, " percent.\n")
produces:
This is 100 percent.
printf(format_string [, obj, ...]) → nil
Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf for details.
putc(obj) → obj
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.
$stdout.putc "A"
$stdout.putc 65
produces:
AA
puts(obj, ...) → nil
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.
[22] pry(main)* ARGF.skip, "a", "test")
[22] pry(main)* ARGF.filename #=> "glark"
[22] pry(main)* tell → Integer
[22] pry(main)* pos → Integer
[22] pry(main)* Returns the current offset (in bytes) of the current file in ARGF.
[22] pry(main)*
[22] pry(main)* ARGF.pos #=> 0
[22] pry(main)* ARGF.gets #=> "This is line one\n"
[22] pry(main)* ARGF.pos #=> 17
[22] pry(main)* pos = position → Integer
[22] pry(main)* Seeks to the position given by position (in bytes) in ARGF.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.pos = 17
[22] pry(main)* ARGF.gets #=> "This is line two\n"
[22] pry(main)* print() → nil
[22] pry(main)* print(obj, ...) → nil
[22] pry(main)* 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 ($\</code>) is not <code>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.
[22] pry(main)*
[22] pry(main)* $stdout.print("This is ", 100, " percent.\n")
[22] pry(main)* produces:
[22] pry(main)*
[22] pry(main)* This is 100 percent.
a
test
read([length [, buffer]]) → string, buffer, or nil
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 _buffer_ argument is present, it must reference a String,
which will receive the data.
For example:
$ echo "small" > small.txt
$ echo "large" > large.txt
$ ./glark.rb small.txt large.txt
[22] pry(main)* This is 100 percent.
[22] pry(main)* printf(format_string [, obj, ...]) → nil
[22] pry(main)* Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf for details.
[22] pry(main)*
[22] pry(main)* putc(obj) → obj
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* $stdout.putc "A"
[22] pry(main)* $stdout.putc 65
[22] pry(main)* produces:
[22] pry(main)*
[22] pry(main)* AA
[22] pry(main)* puts(obj, ...) → nil
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* $stdout.puts("this", "is", "a", "test")
[22] pry(main)* produces:
[22] pry(main)*
[22] pry(main)* this
a
test
read([length [, buffer]]) → string, buffer, or nil
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 _buffer_ argument is present, it must reference a String,
which will receive the data.
For example:
$ echo "small" > small.txt
$ echo "large" > large.txt
$ ./glark.rb small.txt large.txt
ARGF.read #=> "small\nlarge"
[22] pry(main)* isin)* is
[22] pry(main)* a
[22] pry(main)* test
[22] pry(main)* read([length [, buffer]]) → string, buffer, or nil
[22] pry(main)* Reads _length_ bytes from ARGF. The files named on the command line
[22] pry(main)* are concatenated and treated as a single file by this method, so when
[22] pry(main)* called without arguments the contents of this pseudo file are returned in
[22] pry(main)* their entirety.
[22] pry(main)*
[22] pry(main)* _length_ must be a non-negative integer or nil. If it is a positive
[22] pry(main)* integer, +read+ tries to read at most _length_ bytes. It returns nil
[22] pry(main)* if an EOF was encountered before anything could be read. Fewer than
[22] pry(main)* _length_ bytes may be returned if an EOF is encountered during the read.
[22] pry(main)*
[22] pry(main)* If _length_ is omitted or is _nil_, it reads until EOF. A String is
[22] pry(main)* returned even if EOF is encountered before any data is read.
[22] pry(main)*
[22] pry(main)* If _length_ is zero, it returns _""_.
[22] pry(main)*
[22] pry(main)* If the optional _buffer_ argument is present, it must reference a String,
[22] pry(main)* which will receive the data.
e"
ARGF.read(2) #=> "sm"
ARGF.read(0) #=> ""
Note that this method behaves like fread() function in C. If you need the
behavior like read(2) system call, consider +ARGF.readpartial+.
read_nonblock(maxlen) → string
read_nonblock(maxlen, outbuf) → outbuf
Reads at most maxlen bytes from the ARGF stream in non-blocking mode.
readbyte → Fixnum
Reads the next 8-bit byte from ARGF and returns it as a Fixnum. Raises an EOFError after the last byte of the last file has been read.
For example:
$ echo "foo" > file
$ ruby argf.rb file
ARGF.readbyte #=> 102
ARGF.readbyte #=> 111
ARGF.readbyte #=> 111
ARGF.readbyte #=> 10
ARGF.readbyte #=> end of file reached (EOFError)
readchar → String or nil
Reads the next character from ARGF and returns it as a String. Raises an EOFError after the last character of the last file has been read.
For example:
$ echo "foo" > file
$ ruby argf.rb file
ARGF.readchar #=> "f"
ARGF.readchar #=> "o"
[22] pry(main)* which will receive the data.
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ echo "small" > small.txt
[22] pry(main)* $ echo "large" > large.txt
[22] pry(main)* $ ./glark.rb small.txt large.txt
[22] pry(main)*
[22] pry(main)* ARGF.read #=> "small\nlarge"
[22] pry(main)* ARGF.read(200) #=> "small\nlarge"
[22] pry(main)* ARGF.read(2) #=> "sm"
[22] pry(main)* ARGF.read(0) #=> ""
[22] pry(main)*
[22] pry(main)* Note that this method behaves like fread() function in C. If you need the
[22] pry(main)* behavior like read(2) system call, consider +ARGF.readpartial+.
[22] pry(main)* read_nonblock(maxlen) → string
[22] pry(main)* read_nonblock(maxlen, outbuf) → outbuf
[22] pry(main)* Reads at most maxlen bytes from the ARGF stream in non-blocking mode.
[22] pry(main)*
[22] pry(main)* readbyte → Fixnum
[22] pry(main)* Reads the next 8-bit byte from ARGF and returns it as a Fixnum. Raises an EOFError after the last byte of the last file has been read.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ echo "foo" > file
Error: Couldn't locate a definition for echo "foo" > file!
[22] pry(main)* $ ruby argf.rb file
#=> end of file reached (EOFError)
readline(sep=$/) → string
readline(limit) → string
readline(sep, limit) → string
Returns the next line from the current file in ARGF.
By default lines are assumed to be separated by +$/+; to use a different character as a separator, supply it as a String for the sep argument.
The optional limit argument specifies how many characters of each line to return. By default all characters are returned.
An EOFError is raised at the end of the file.
readlines(sep=$/) → array
readlines(limit) → array
readlines(sep, limit) → array
to_a(sep=$/) → array
to_a(limit) → array
to_a(sep, limit) → array
Reads ARGF‘s current file in its entirety, returning an Array of its lines, one line per element. Lines are assumed to be separated by sep.
lines = ARGF.readlines
lines[0] #=> "This is line one\n"
readpartial(maxlen) → string
readpartial(maxlen, outbuf) → outbuf
Reads at most maxlen bytes from the ARGF stream. It blocks only ifError: Couldn't locate a definition for ruby argf.rb file!
[22] pry(main)*
[22] pry(main)* ARGF.readbyte #=> 102
[22] pry(main)* ARGF.readbyte #=> 111
[22] pry(main)* ARGF.readbyte #=> 111
[22] pry(main)* ARGF.readbyte #=> 10
[22] pry(main)* ARGF.readbyte #=> end of file reached (EOFError)
[22] pry(main)* readchar → String or nil
[22] pry(main)* Reads the next character from ARGF and returns it as a String. Raises an EOFError after the last character of the last file has been read.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ echo "foo" > file
Error: Couldn't locate a definition for echo "foo" > file!
[22] pry(main)* $ ruby argf.rb file
Error: Couldn't locate a definition for ruby argf.rb file!
[22] pry(main)*
[22] pry(main)* ARGF.readchar #=> "f"
[22] pry(main)* ARGF.readchar #=> "o"
[22] pry(main)* ARGF.readchar #=> "o"
[22] pry(main)* ARGF.readchar #=> "\n"
[22] pry(main)* ARGF.readchar #=> end of file reached (EOFError)
[22] pry(main)* readline(sep=$/) → string
[22] pry(main)* readline(limit) → string
[22] pry(main)* readline(sep, limit) → string
[22] pry(main)* Returns the next line from the current file in ARGF.
[22] pry(main)*
[22] pry(main)* By default lines are assumed to be separated by +$/+; to use a different character as a separator, supply it as a String for the sep argument.
[22] pry(main)*
[22] pry(main)* The optional limit argument specifies how many characters of each line to return. By default all characters are returned.
[22] pry(main)*
[22] pry(main)* An EOFError is raised at the end of the file.
[22] pry(main)*
[22] pry(main)* readlines(sep=$/) → array
[22] pry(main)* readlines(limit) → array
[22] pry(main)* readlines(sep, limit) → array
[22] pry(main)* to_a(sep=$/) → array
[22] pry(main)* to_a(limit) → array
[22] pry(main)* to_a(sep, limit) → array
[22] pry(main)* Reads ARGF‘s current file in its entirety, returning an Array of its lines, one line per element. Lines are assumed to be separated by sep.
[22] pry(main)*
[22] pry(main)* lines = ARGF.readlines
[22] pry(main)* lines[0] #=> "This is line one\n"
diately available. If the optional outbuf argument is present, it must reference a String, which will receive the data. 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.
rewind → 0
Positions the current file to the beginning of input, resetting ARGF.lineno to zero.
[22] pry(main)* readpartial(maxlen) → stringrtial(maxlen) → string
[22] pry(main)* readpartial(maxlen, outbuf) → outbuf
[22] pry(main)* 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. It raises EOFError on end of file.
[22] pry(main)*
[22] pry(main)* 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:
[22] pry(main)*
[22] pry(main)* The byte buffer in the IO object is empty.
[22] pry(main)*
[22] pry(main)* The content of the stream is empty.
[22] pry(main)*
[22] pry(main)* The stream has not reached EOF.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* rewind → 0
[22] pry(main)* Positions the current file to the beginning of input, resetting ARGF.lineno to zero.
[22] pry(main)*
n"
ARGF.rewind #=> 0
ARGF.lineno #=> 0
ARGF.readline #=> "This is line one\n"
seek(amount, whence=IO::SEEK_SET) → 0
Seeks to offset amount (an Integer) in the ARGF stream according to the value of whence. See +IO#seek+ for further details.
set_encoding(ext_enc) → ARGF
set_encoding("ext_enc:int_enc") → ARGF
set_encoding(ext_enc, int_enc) → ARGF
set_encoding("ext_enc:int_enc", opt) → ARGF
set_encoding(ext_enc, int_enc, opt) → ARGF
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.
[22] pry(main)* ARGF.readline #=> "This is line one\n")* ARGF.readline #=> "This is line one\n"
[22] pry(main)* ARGF.rewind #=> 0
[22] pry(main)* ARGF.lineno #=> 0
[22] pry(main)* ARGF.readline #=> "This is line one\n"
[22] pry(main)* seek(amount, whence=IO::SEEK_SET) → 0
[22] pry(main)* Seeks to offset amount (an Integer) in the ARGF stream according to the value of whence. See +IO#seek+ for further details.
[22] pry(main)*
[22] pry(main)* set_encoding(ext_enc) → ARGF
[22] pry(main)* set_encoding("ext_enc:int_enc") → ARGF
[22] pry(main)* set_encoding(ext_enc, int_enc) → ARGF
[22] pry(main)* set_encoding("ext_enc:int_enc", opt) → ARGF
[22] pry(main)* set_encoding(ext_enc, int_enc, opt) → ARGF
[22] pry(main)* If single argument is specified, strings read from ARGF are tagged with the encoding specified.
[22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* 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.
ified, 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 text
ARGF.set_encoding(Encoding::UTF_8) # Tag the input as UTF-8 text
ARGF.set_encoding('utf-8','ascii') # Transcode the input from US-ASCII
# to UTF-8.
skip → ARGF
Sets the current file to the next file in ARGV. If there aren't any more
files it has no effect.
For example:
$ ruby argf.rb foo bar
ARGF.filename #=> "foo"
ARGF.skip
ARGF.filename #=> "bar"
tell → Integer
pos → Integer
Returns the current offset (in bytes) of the current file in ARGF.
ARGF.pos #=> 0
ARGF.gets #=> "This is line one\n"
ARGF.pos #=> 17
readlines(sep=$/) → array
readlines(limit) → array
readlines(sep, limit) → array
to_a(sep=$/) → array
to_a(limit) → array
to_a(sep, limit) → array
[22] pry(main)* rent file [22] pry(main)*
[22] pry(main)* 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.
[22] pry(main)*
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* ARGF.set_encoding('ascii') # Tag the input as US-ASCII text
[22] pry(main)* ARGF.set_encoding(Encoding::UTF_8) # Tag the input as UTF-8 text
[22] pry(main)* ARGF.set_encoding('utf-8','ascii') # Transcode the input from US-ASCII
[22] pry(main)* # to UTF-8.
[22] pry(main)* skip → ARGF
[22] pry(main)* Sets the current file to the next file in ARGV. If there aren't any more
[22] pry(main)* files it has no effect.
[22] pry(main)* For example:
[22] pry(main)*
[22] pry(main)* $ ruby argf.rb foo bar
Error: Couldn't locate a definition for ruby argf.rb foo bar!
[22] pry(main)* ARGF.filename #=> "foo"
[22] pry(main)* ARGF.skip
[22] pry(main)* ARGF.filename #=> "bar"
[22] pry(main)* tell → Integer
[22] pry(main)* pos → Integer
[22] pry(main)* Returns the current offset (in bytes) of the current file in ARGF.
[22] pry(main)*
[22] pry(main)* ARGF.pos #=> 0
n Array of its lines, one line per element. Lines are assumed to be separated by sep.
lines = ARGF.readlines
lines[0] #=> "This is line one\n"
fileno → fixnum
to_i → fixnum
Returns an integer representing the numeric file descriptor for the current file. Raises an ArgumentError if there isn’t a current file.
ARGF.fileno #=> 3
to_io → IO
Returns an IO object representing the current file. This will be a File object unless the current file is a stream such as STDIN.
For example:
ARGF.to_io #=> #<File:glark.txt>
ARGF.to_io #=> #<IO:<STDIN>>
to_s → String
Returns “ARGF”.
to_write_io → io
Returns IO instance tied to ARGF for writing if inplace mode is enabled.
write(string) → integer
Writes string if inplace mode.
Commenting is here to help enhance the documentation. For example, sample code, or clarification of the documentation.
[22] pry(main)* ARGF.gets #=> "This is line one\n"ease wrap them in "<pre><code class="ruby" > ... </code><[22] pry(main)* ARGF.gets #=> "This is line one\n"
[22] pry(main)* ARGF.pos #=> 17
[22] pry(main)* readlines(sep=$/) → array
[22] pry(main)* readlines(limit) → array
[22] pry(main)* readlines(sep, limit) → array
[22] pry(main)* to_a(sep=$/) → array
[22] pry(main)* to_a(limit) → array
[22] pry(main)* to_a(sep, limit) → array
[22] pry(main)* Reads ARGF‘s current file in its entirety, returning an Array of its lines, one line per element. Lines are assumed to be separated by sep.
[22] pry(main)*
[22] pry(main)* lines = ARGF.readlines
[22] pry(main)* lines[0] #=> "This is line one\n"
[22] pry(main)* fileno → fixnum
[22] pry(main)* to_i → fixnum
[22] pry(main)* Returns an integer representing the numeric file descriptor for the current file. Raises an ArgumentError if there isn’t a current file.
[22] pry(main)*
[22] pry(main)* ARGF.fileno #=> 3
[22] pry(main)* to_io → IO
[22] pry(main)* Returns an IO object representing the current file. This will be a File object unless the current file is a stream such as STDIN.
[22] pry(main)*
get syntax highlighting.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
[22] pry(main)*
[22] pry(main)* For example:tion of the docs, please do so, but also file a bug report so that it can be corrected for the next release. Thank you.
[22] pry(main)*
[22] pry(main)* ARGF.to_io #=> #<File:glark.txt>
[22] pry(main)* ARGF.to_io #=> #<IO:<STDIN>>
[22] pry(main)* to_s → String
[22] pry(main)* Returns “ARGF”.
[22] pry(main)*
[22] pry(main)* to_write_io → io
[22] pry(main)* Returns IO instance tied to ARGF for writing if inplace mode is enabled.
[22] pry(main)*
[22] pry(main)* write(string) → integer
[22] pry(main)* Writes string if inplace mode.
[22] pry(main)*
[22] pry(main)* Commenting is here to help enhance the documentation. For example, sample code, or clarification of the documentation.
[22] pry(main)*
[22] pry(main)* If you are posting code samples in your comments, please wrap them in "<pre><code class="ruby" > ... </code></pre>" markup in order to get syntax highlighting.
[22] pry(main)*
[22] pry(main)* If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
[22] pry(main)*
[22] pry(main)* If you wish to post a correction of the docs, please do so, but also file a bug report so that it can be corrected for the next release. Thank you.
[22] pry(main)* EOF
=> "ARGF\nARGF is a stream designed for use in scripts that process files given as command-line arguments or passed in via STDIN.\nThe 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:\nARGV #=> [\"--verbose\", \"file1\", \"file2\"]\noption = ARGV.shift #=> \"--verbose\"\nARGV #=> [\"file1\", \"file2\"]\nYou 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.\nAfter a file in ARGV has been read ARGF removes it from the Array. Thus, after all files have been read ARGV will be empty.\nYou 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:\nARGV.replace [\"file1\"]\nARGF.readlines # Returns the contents of file1 as an Array\nARGV #=> []\nARGV.replace [\"file2\", \"file3\"]\nARGF.read # Returns the contents of file2 and file3\nIf ARGV is empty, ARGF acts as if it contained STDIN, i.e. the data piped to your script. For example:\n\"glark\\n\"\nPublic Instance Methods\nargv → ARGV\nReturns the ARGV array, which contains the arguments passed to your script, one per element.\nFor example:\nARGF.argv #=> [\"-v\", \"glark.txt\"]\nbinmode → ARGF\nPuts 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:\nNewline conversion is disabled.\nEncoding conversion is disabled.\nContent is treated as ASCII-8BIT.\nbinmode? → true or false\nReturns true if +ARGF+ is being read in binary mode; false otherwise. (To\nenable binary mode use +ARGF.binmode+.\nFor example:\nARGF.binmode? #=> false\nARGF.binmode\nARGF.binmode? #=> true\nbytes {|byte| block } → ARGF\nbytes → an_enumerator\neach_byte {|byte| block } → ARGF\neach_byte → an_enumerator\nIterates over each byte of each file in +ARGV+.\nA byte is returned as a +Fixnum+ in the range 0..255.\nThis method allows you to treat the files supplied on the command line as\na single file consisting of the concatenation of each named file. After\nthe last byte of the first file has been returned, the first byte of the\nsecond file is returned. The +ARGF.filename+ method can be used to\ndetermine the filename of the current byte.\nIf no block is given, an enumerator is returned instead.\nFor example:\nARGF.bytes.to_a #=> [35, 32, ... 95, 10]\nchars {|char| block } → ARGF\nchars → an_enumerator\neach_char {|char| block } → ARGF\neach_char → an_enumerator\nIterates over each character of each file in ARGF.\nThis 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.\nIf no block is given, an enumerator is returned instead.\nclose → ARGF\nCloses the current file and skips to the next in the stream. Trying to\nclose a file that has already been closed causes an +IOError+ to be\nraised.\nFor example:\nARGF.filename #=> \"foo\"\nARGF.close\nARGF.filename #=> \"bar\"\nARGF.close\nARGF.close #=> closed stream (IOError)\nclosed? → true or false\nReturns true if the current file has been closed; false otherwise. Use ARGF.close to actually close the current file.\ncodepoints {|codepoint| block } → ARGF\ncodepoints → an_enumerator\neach_codepoint {|codepoint| block } → ARGF\neach_codepoint → an_enumerator\nIterates over each codepoint of each file in ARGF.\nThis 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.\nIf no block is given, an enumerator is returned instead.\neach(sep=$/) {|line| block } → ARGF\neach(sep=$/,limit) {|line| block } → ARGF\neach(...) → an_enumerator\neach_line(sep=$/) {|line| block } → ARGF\neach_line(sep=$/,limit) {|line| block } → ARGF\neach_line(...) → an_enumerator\nlines(sep=$/) {|line| block } → ARGF\nlines(sep=$/,limit) {|line| block } → ARGF\nlines(...) → an_enumerator\nReturns 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.\nThis 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.\nFor example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:\nARGF.lines do |line|\n puts ARGF.filename if ARGF.lineno == 1\n puts \"\#{ARGF.lineno}: \#{line}\"\nend\nbytes {|byte| block } → ARGF\nbytes → an_enumerator\neach_byte {|byte| block } → ARGF\neach_byte → an_enumerator\nIterates over each byte of each file in +ARGV+.\nA byte is returned as a +Fixnum+ in the range 0..255.\nThis method allows you to treat the files supplied on the command line as\na single file consisting of the concatenation of each named file. After\nthe last byte of the first file has been returned, the first byte of the\nsecond file is returned. The +ARGF.filename+ method can be used to\ndetermine the filename of the current byte.\nIf no block is given, an enumerator is returned instead.\nFor example:\nARGF.bytes.to_a #=> [35, 32, ... 95, 10]\nchars {|char| block } → ARGF\nchars → an_enumerator\neach_char {|char| block } → ARGF\neach_char → an_enumerator\nIterates over each character of each file in ARGF.\nThis 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.\nIf no block is given, an enumerator is returned instead.\ncodepoints {|codepoint| block } → ARGF\ncodepoints → an_enumerator\neach_codepoint {|codepoint| block } → ARGF\neach_codepoint → an_enumerator\nIterates over each codepoint of each file in ARGF.\nThis 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.\nIf no block is given, an enumerator is returned instead.\neach(sep=$/) {|line| block } → ARGF\neach(sep=$/,limit) {|line| block } → ARGF\neach(...) → an_enumerator\neach_line(sep=$/) {|line| block } → ARGF\neach_line(sep=$/,limit) {|line| block } → ARGF\neach_line(...) → an_enumerator\nlines(sep=$/) {|line| block } → ARGF\nlines(sep=$/,limit) {|line| block } → ARGF\nlines(...) → an_enumerator\nReturns 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.\nThis 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.\nFor example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:\nARGF.lines do |line|\n puts ARGF.filename if ARGF.lineno == 1\n puts \"\#{ARGF.lineno}: \#{line}\"\nend\neof? → true or false\neof → true or false\nReturns true if the current file in ARGF is at end of file, i.e. it has no data to read. The stream must be opened for reading or an IOError will be raised.\nARGF.eof? #=> false\n3.times { ARGF.readchar }\nARGF.eof? #=> false\nARGF.readchar #=> \"\\n\"\nARGF.eof? #=> true\neof? → true or false\neof → true or false\nReturns true if the current file in ARGF is at end of file, i.e. it has no data to read. The stream must be opened for reading or an IOError will be raised.\nARGF.eof? #=> false\n3.times { ARGF.readchar }\nARGF.eof? #=> false\nARGF.readchar #=> \"\\n\"\nARGF.eof? #=> true\nexternal_encoding → encoding\nReturns the external encoding for files read from +ARGF+ as an +Encoding+\nobject. The external encoding is the encoding of the text as stored in a\nfile. Contrast with +ARGF.internal_encoding+, which is the encoding used\nto represent this text within Ruby.\nTo set the external encoding use +ARGF.set_encoding+.\nFor example:\nARGF.external_encoding #=> #<Encoding:UTF-8>\nfile → IO or File object\nReturns the current file as an IO or File object. #<IO:<STDIN>> is returned when the current file is STDIN.\nFor example:\nARGF.file #=> #<File:foo>\nARGF.read(5) #=> \"foo\\nb\"\nARGF.file #=> #<File:bar>\nfilename → String\npath → String\nReturns the current filename. “-” is returned when the current file is STDIN.\nFor example:\nARGF.filename #=> \"foo\"\nARGF.read(5) #=> \"foo\\nb\"\nARGF.filename #=> \"bar\"\nARGF.skip\nARGF.filename #=> \"glark\"\nfileno → fixnum\nto_i → fixnum\nReturns an integer representing the numeric file descriptor for the current file. Raises an ArgumentError if there isn’t a current file.\nARGF.fileno #=> 3\ngetbyte → Fixnum or nil\nGets the next 8-bit byte (0..255) from ARGF. Returns nil if called at the end of the stream.\nFor example:\nARGF.getbyte #=> 102\nARGF.getbyte #=> 111\nARGF.getbyte #=> 111\nARGF.getbyte #=> 10\nARGF.getbyte #=> nil\ngetc → String or nil\nReads the next character from ARGF and returns it as a String. Returns nil at the end of the stream.\nARGF 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.\nFor example:\nARGF.getc #=> \"f\"\nARGF.getc #=> \"o\"\nARGF.getc #=> \"o\"\nARGF.getc #=> \"\\n\"\nARGF.getc #=> nil\nARGF.getc #=> nil\ngets(sep=$/) → string\ngets(limit) → string\ngets(sep, limit) → string\nReturns the next line from the current file in ARGF.\nBy default lines are assumed to be separated by +$/+; to use a different character as a separator, supply it as a String for the sep argument.\nThe optional limit argument specifies how many characters of each line to return. By default all characters are returned.\ninplace_mode → String\nReturns the file extension appended to the names of modified files under inplace-edit mode. This value can be set using ARGF.inplace_mode= or passing the -i switch to the Ruby binary.\ninplace_mode = ext → ARGF\nSets the filename extension for inplace editing mode to the given String.\nEach file being edited has this value appended to its filename. The\nmodified file is saved under this new name.\nFor example:\n $ ruby argf.rb file.txt\n ARGF.inplace_mode = '.bak'\n ARGF.lines do |line|\n print line.sub(\"foo\",\"bar\")\n end\nEach line of file.txt has the first occurrence of “foo” replaced with “bar”, then the new line is written out to file.txt.bak.\ninternal_encoding → encoding\nReturns the internal encoding for strings read from ARGF as an Encoding object.\nIf 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.\nlineno → integer\nReturns the current line number of ARGF as a whole. This value can be set manually with ARGF.lineno=.\nFor example:\nARGF.lineno #=> 0\nARGF.readline #=> \"This is line 1\\n\"\nARGF.lineno #=> 1\nlineno = number → nil\nSets the line number of ARGF as a whole to the given Integer.\nARGF sets the line number automatically as you read data, so normally you will not need to set it explicitly. To access the current line number use ARGF.lineno.\nFor example:\nARGF.lineno #=> 0\nARGF.readline #=> \"This is line 1\\n\"\nARGF.lineno #=> 1\nARGF.lineno = 0 #=> nil\nARGF.lineno #=> 0\neach(sep=$/) {|line| block } → ARGF\neach(sep=$/,limit) {|line| block } → ARGF\neach(...) → an_enumerator\neach_line(sep=$/) {|line| block } → ARGF\neach_line(sep=$/,limit) {|line| block } → ARGF\neach_line(...) → an_enumerator\nlines(sep=$/) {|line| block } → ARGF\nlines(sep=$/,limit) {|line| block } → ARGF\nlines(...) → an_enumerator\nReturns 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.\nThis 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.\nFor example, the following code prints out each line of each named file prefixed with its line number, displaying the filename once per file:\nARGF.lines do |line|\n puts ARGF.filename if ARGF.lineno == 1\n puts \"\#{ARGF.lineno}: \#{line}\"\nend\nfilename → String\npath → String\nReturns the current filename. “-” is returned when the current file is STDIN.\nFor example:\nARGF.filename #=> \"foo\"\nARGF.read(5) #=> \"foo\\nb\"\nARGF.filename #=> \"bar\"\nARGF.skip\nARGF.filename #=> \"glark\"\ntell → Integer\npos → Integer\nReturns the current offset (in bytes) of the current file in ARGF.\nARGF.pos #=> 0\nARGF.gets #=> \"This is line one\\n\"\nARGF.pos #=> 17\npos = position → Integer\nSeeks to the position given by position (in bytes) in ARGF.\nFor example:\nARGF.pos = 17\nARGF.gets #=> \"This is line two\\n\"\nprint() → nil\nprint(obj, ...) → nil\nWrites 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 ($\\</code>) is not <code>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.\n$stdout.print(\"This is \", 100, \" percent.\\n\")\nproduces:\nThis is 100 percent.\nprintf(format_string [, obj, ...]) → nil\nFormats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf for details.\nputc(obj) → obj\nIf 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.\n$stdout.putc \"A\"\n$stdout.putc 65\nproduces:\nAA\nputs(obj, ...) → nil\nWrites 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.\n$stdout.puts(\"this\", \"is\", \"a\", \"test\")\nproduces:\nthis\nis\na\ntest\nread([length [, buffer]]) → string, buffer, or nil\nReads _length_ bytes from ARGF. The files named on the command line\nare concatenated and treated as a single file by this method, so when\ncalled without arguments the contents of this pseudo file are returned in\ntheir entirety.\n_length_ must be a non-negative integer or nil. If it is a positive\ninteger, +read+ tries to read at most _length_ bytes. It returns nil\nif an EOF was encountered before anything could be read. Fewer than\n_length_ bytes may be returned if an EOF is encountered during the read.\nIf _length_ is omitted or is _nil_, it reads until EOF. A String is\nreturned even if EOF is encountered before any data is read.\nIf _length_ is zero, it returns _\"\"_.\nIf the optional _buffer_ argument is present, it must reference a String,\nwhich will receive the data.\nFor example:\n $ echo \"small\" > small.txt\n $ echo \"large\" > large.txt\n $ ./glark.rb small.txt large.txt\n ARGF.read #=> \"small\\nlarge\"\n ARGF.read(200) #=> \"small\\nlarge\"\n ARGF.read(2) #=> \"sm\"\n ARGF.read(0) #=> \"\"\nNote that this method behaves like fread() function in C. If you need the\nbehavior like read(2) system call, consider +ARGF.readpartial+.\nread_nonblock(maxlen) → string\nread_nonblock(maxlen, outbuf) → outbuf\nReads at most maxlen bytes from the ARGF stream in non-blocking mode.\nreadbyte → Fixnum\nReads the next 8-bit byte from ARGF and returns it as a Fixnum. Raises an EOFError after the last byte of the last file has been read.\nFor example:\nARGF.readbyte #=> 102\nARGF.readbyte #=> 111\nARGF.readbyte #=> 111\nARGF.readbyte #=> 10\nARGF.readbyte #=> end of file reached (EOFError)\nreadchar → String or nil\nReads the next character from ARGF and returns it as a String. Raises an EOFError after the last character of the last file has been read.\nFor example:\nARGF.readchar #=> \"f\"\nARGF.readchar #=> \"o\"\nARGF.readchar #=> \"o\"\nARGF.readchar #=> \"\\n\"\nARGF.readchar #=> end of file reached (EOFError)\nreadline(sep=$/) → string\nreadline(limit) → string\nreadline(sep, limit) → string\nReturns the next line from the current file in ARGF.\nBy default lines are assumed to be separated by +$/+; to use a different character as a separator, supply it as a String for the sep argument.\nThe optional limit argument specifies how many characters of each line to return. By default all characters are returned.\nAn EOFError is raised at the end of the file.\nreadlines(sep=$/) → array\nreadlines(limit) → array\nreadlines(sep, limit) → array\nto_a(sep=$/) → array\nto_a(limit) → array\nto_a(sep, limit) → array\nReads ARGF‘s current file in its entirety, returning an Array of its lines, one line per element. Lines are assumed to be separated by sep.\nlines = ARGF.readlines\nlines[0] #=> \"This is line one\\n\"\nreadpartial(maxlen) → string\nreadpartial(maxlen, outbuf) → outbuf\nReads 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. It raises EOFError on end of file.\nreadpartial 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:\nThe byte buffer in the IO object is empty.\nThe content of the stream is empty.\nThe stream has not reached EOF.\nWhen 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.\nWhen 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.\nrewind → 0\nPositions the current file to the beginning of input, resetting ARGF.lineno to zero.\nARGF.readline #=> \"This is line one\\n\"\nARGF.rewind #=> 0\nARGF.lineno #=> 0\nARGF.readline #=> \"This is line one\\n\"\nseek(amount, whence=IO::SEEK_SET) → 0\nSeeks to offset amount (an Integer) in the ARGF stream according to the value of whence. See +IO#seek+ for further details.\nset_encoding(ext_enc) → ARGF\nset_encoding(\"ext_enc:int_enc\") → ARGF\nset_encoding(ext_enc, int_enc) → ARGF\nset_encoding(\"ext_enc:int_enc\", opt) → ARGF\nset_encoding(ext_enc, int_enc, opt) → ARGF\nIf single argument is specified, strings read from ARGF are tagged with the encoding specified.\nIf 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.\nIf 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.\nIf 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.\nFor example:\nARGF.set_encoding('ascii') # Tag the input as US-ASCII text\nARGF.set_encoding(Encoding::UTF_8) # Tag the input as UTF-8 text\nARGF.set_encoding('utf-8','ascii') # Transcode the input from US-ASCII\n # to UTF-8.\nskip → ARGF\nSets the current file to the next file in ARGV. If there aren't any more\nfiles it has no effect.\nFor example:\nARGF.filename #=> \"foo\"\nARGF.skip\nARGF.filename #=> \"bar\"\ntell → Integer\npos → Integer\nReturns the current offset (in bytes) of the current file in ARGF.\nARGF.pos #=> 0\nARGF.gets #=> \"This is line one\\n\"\nARGF.pos #=> 17\nreadlines(sep=$/) → array\nreadlines(limit) → array\nreadlines(sep, limit) → array\nto_a(sep=$/) → array\nto_a(limit) → array\nto_a(sep, limit) → array\nReads ARGF‘s current file in its entirety, returning an Array of its lines, one line per element. Lines are assumed to be separated by sep.\nlines = ARGF.readlines\nlines[0] #=> \"This is line one\\n\"\nfileno → fixnum\nto_i → fixnum\nReturns an integer representing the numeric file descriptor for the current file. Raises an ArgumentError if there isn’t a current file.\nARGF.fileno #=> 3\nto_io → IO\nReturns an IO object representing the current file. This will be a File object unless the current file is a stream such as STDIN.\nFor example:\nARGF.to_io #=> #<File:glark.txt>\nARGF.to_io #=> #<IO:<STDIN>>\nto_s → String\nReturns “ARGF”.\nto_write_io → io\nReturns IO instance tied to ARGF for writing if inplace mode is enabled.\nwrite(string) → integer\nWrites string if inplace mode.\nCommenting is here to help enhance the documentation. For example, sample code, or clarification of the documentation.\nIf you are posting code samples in your comments, please wrap them in \"<pre><code class=\"ruby\" > ... </code></pre>\" markup in order to get syntax highlighting.\nIf you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.\nIf you wish to post a correction of the docs, please do so, but also file a bug report so that it can be corrected for the next release. Thank you.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment