Class: ARGF

Inherits:
Object show all
Includes:
Enumerable
Defined in:
io.c

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #find, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #include?, #inject, #map, #max, #max_by, #member?, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #slice_before, #sort, #sort_by, #take, #take_while, #zip

Constructor Details

#initializeObject

:nodoc:



# File 'io.c'

/* :nodoc: */
static VALUE
argf_initialize(VALUE argf, VALUE argv)
{
    memset(&ARGF, 0, sizeof(ARGF));
    argf_init(&ARGF, argv);

    return argf;
}

Instance Method Details

#argvObject

Returns the ARGV array, which contains the arguments passed to your script, one per element.

For example:

$ ruby argf.rb -v glark.txt

ARGF.argv   #=> ["-v", "glark.txt"]


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.argv  -> ARGV
 *
 *  Returns the +ARGV+ array, which contains the arguments passed to your
 *  script, one per element.
 *
 *  For example:
 *
 *      $ ruby argf.rb -v glark.txt
 *
 *      ARGF.argv   #=> ["-v", "glark.txt"]
 *
 */
static VALUE
argf_argv(VALUE argf)
{
    return ARGF.argv;
}

#binmodeObject

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:

  • Newline conversion is disabled.

  • Encoding conversion is disabled.

  • Content is treated as ASCII-8BIT.



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.binmode  -> ARGF
 *
 *  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:
 *
 *  *  Newline conversion is disabled.
 *  *  Encoding conversion is disabled.
 *  *  Content is treated as ASCII-8BIT.
 */
static VALUE
argf_binmode_m(VALUE argf)
{
    ARGF.binmode = 1;
    next_argv();
    ARGF_FORWARD(0, 0);
    rb_io_ascii8bit_binmode(ARGF.current_file);
    return argf;
}

#binmode?Boolean

Returns true if ARGF is being read in binary mode; false otherwise. (To enable binary mode use ARGF.binmode.

For example:

ARGF.binmode?  #=> false
ARGF.binmode
ARGF.binmode?  #=> true

Returns:

  • (Boolean)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.binmode?  -> true or false
 *
 *  Returns true if +ARGF+ is being read in binary mode; false otherwise. (To
 *  enable binary mode use +ARGF.binmode+.
 *
 * For example:
 *
 *     ARGF.binmode?  #=> false
 *     ARGF.binmode
 *     ARGF.binmode?  #=> true
 */
static VALUE
argf_binmode_p(VALUE argf)
{
    return ARGF.binmode ? Qtrue : Qfalse;
}

#bytes {|byte| ... } ⇒ Object #bytesObject

ARGF.each_byte {|byte| block } -> ARGF

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]

Overloads:

  • #bytes {|byte| ... } ⇒ Object

    Yields:

    • (byte)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.bytes     {|byte| block }  -> ARGF
 *     ARGF.bytes                      -> an_enumerator
 *
 *     ARGF.each_byte {|byte| block }  -> ARGF
 *     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]
 *
 */
static VALUE
argf_each_byte(VALUE argf)
{
    RETURN_ENUMERATOR(argf, 0, 0);
    for (;;) {
    if (!next_argv()) return argf;
    rb_block_call(ARGF.current_file, rb_intern("each_byte"), 0, 0, 0, 0);
    ARGF.next_p = 1;
    }
}

#chars {|char| ... } ⇒ Object #charsObject

ARGF.each_char {|char| block } -> ARGF

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.

Overloads:

  • #chars {|char| ... } ⇒ Object

    Yields:

    • (char)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.chars      {|char| block }  -> ARGF
 *     ARGF.chars                       -> an_enumerator
 *
 *     ARGF.each_char  {|char| block }  -> ARGF
 *     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.
 */
static VALUE
argf_each_char(VALUE argf)
{
    RETURN_ENUMERATOR(argf, 0, 0);
    for (;;) {
    if (!next_argv()) return argf;
    rb_block_call(ARGF.current_file, rb_intern("each_char"), 0, 0, 0, 0);
    ARGF.next_p = 1;
    }
}

#closeObject

Closes the current file and skips to the next in the stream. Trying to close a file that has already been closed causes an IOError to be raised.

For example:

$ ruby argf.rb foo bar

ARGF.filename  #=> "foo"
ARGF.close
ARGF.filename  #=> "bar"
ARGF.close
ARGF.close     #=> closed stream (IOError)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.close  -> ARGF
 *
 *  Closes the current file and skips to the next in the stream. Trying to
 *  close a file that has already been closed causes an +IOError+ to be
 *  raised.
 *
 * For example:
 *
 *     $ ruby argf.rb foo bar
 *
 *     ARGF.filename  #=> "foo"
 *     ARGF.close
 *     ARGF.filename  #=> "bar"
 *     ARGF.close
 *     ARGF.close     #=> closed stream (IOError)
 */
static VALUE
argf_close_m(VALUE argf)
{
    next_argv();
    argf_close(ARGF.current_file);
    if (ARGF.next_p != -1) {
    ARGF.next_p = 1;
    }
    ARGF.lineno = 0;
    return argf;
}

#closed?Boolean

Returns true if the current file has been closed; false otherwise. Use ARGF.close to actually close the current file.

Returns:

  • (Boolean)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.closed?  -> true or false
 *
 *  Returns _true_ if the current file has been closed; _false_ otherwise. Use
 *  +ARGF.close+ to actually close the current file.
 */
static VALUE
argf_closed(VALUE argf)
{
    next_argv();
    ARGF_FORWARD(0, 0);
    return rb_io_closed(ARGF.current_file);
}

#each(sep = $/) {|line| ... } ⇒ Object #each(sep = $/, limit) {|line| ... } ⇒ Object #each(...) ⇒ Object

ARGF.each_line(sep=$/) {|line| block } -> ARGF

ARGF.each_line(sep=$/,limit) {|line| block }  -> ARGF
ARGF.each_line(...)                           -> an_enumerator

ARGF.lines(sep=$/)           {|line| block }   -> ARGF
ARGF.lines(sep=$/,limit)     {|line| block }   -> ARGF
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 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

Overloads:

  • #each(sep = $/) {|line| ... } ⇒ Object

    Yields:

    • (line)
  • #each(sep = $/, limit) {|line| ... } ⇒ Object

    Yields:

    • (line)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.each(sep=$/)            {|line| block }  -> ARGF
 *     ARGF.each(sep=$/,limit)      {|line| block }  -> ARGF
 *     ARGF.each(...)                                -> an_enumerator
 *
 *     ARGF.each_line(sep=$/)       {|line| block }  -> ARGF
 *     ARGF.each_line(sep=$/,limit) {|line| block }  -> ARGF
 *     ARGF.each_line(...)                           -> an_enumerator
 *
 *     ARGF.lines(sep=$/)           {|line| block }   -> ARGF
 *     ARGF.lines(sep=$/,limit)     {|line| block }   -> ARGF
 *     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 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
 */
static VALUE
argf_each_line(int argc, VALUE *argv, VALUE argf)
{
    RETURN_ENUMERATOR(argf, argc, argv);
    for (;;) {
    if (!next_argv()) return argf;
    rb_block_call(ARGF.current_file, rb_intern("each_line"), argc, argv, 0, 0);
    ARGF.next_p = 1;
    }
}

#bytes {|byte| ... } ⇒ Object #bytesObject

ARGF.each_byte {|byte| block } -> ARGF

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]

Overloads:

  • #bytes {|byte| ... } ⇒ Object

    Yields:

    • (byte)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.bytes     {|byte| block }  -> ARGF
 *     ARGF.bytes                      -> an_enumerator
 *
 *     ARGF.each_byte {|byte| block }  -> ARGF
 *     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]
 *
 */
static VALUE
argf_each_byte(VALUE argf)
{
    RETURN_ENUMERATOR(argf, 0, 0);
    for (;;) {
    if (!next_argv()) return argf;
    rb_block_call(ARGF.current_file, rb_intern("each_byte"), 0, 0, 0, 0);
    ARGF.next_p = 1;
    }
}

#chars {|char| ... } ⇒ Object #charsObject

ARGF.each_char {|char| block } -> ARGF

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.

Overloads:

  • #chars {|char| ... } ⇒ Object

    Yields:

    • (char)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.chars      {|char| block }  -> ARGF
 *     ARGF.chars                       -> an_enumerator
 *
 *     ARGF.each_char  {|char| block }  -> ARGF
 *     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.
 */
static VALUE
argf_each_char(VALUE argf)
{
    RETURN_ENUMERATOR(argf, 0, 0);
    for (;;) {
    if (!next_argv()) return argf;
    rb_block_call(ARGF.current_file, rb_intern("each_char"), 0, 0, 0, 0);
    ARGF.next_p = 1;
    }
}

#each(sep = $/) {|line| ... } ⇒ Object #each(sep = $/, limit) {|line| ... } ⇒ Object #each(...) ⇒ Object

ARGF.each_line(sep=$/) {|line| block } -> ARGF

ARGF.each_line(sep=$/,limit) {|line| block }  -> ARGF
ARGF.each_line(...)                           -> an_enumerator

ARGF.lines(sep=$/)           {|line| block }   -> ARGF
ARGF.lines(sep=$/,limit)     {|line| block }   -> ARGF
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 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

Overloads:

  • #each(sep = $/) {|line| ... } ⇒ Object

    Yields:

    • (line)
  • #each(sep = $/, limit) {|line| ... } ⇒ Object

    Yields:

    • (line)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.each(sep=$/)            {|line| block }  -> ARGF
 *     ARGF.each(sep=$/,limit)      {|line| block }  -> ARGF
 *     ARGF.each(...)                                -> an_enumerator
 *
 *     ARGF.each_line(sep=$/)       {|line| block }  -> ARGF
 *     ARGF.each_line(sep=$/,limit) {|line| block }  -> ARGF
 *     ARGF.each_line(...)                           -> an_enumerator
 *
 *     ARGF.lines(sep=$/)           {|line| block }   -> ARGF
 *     ARGF.lines(sep=$/,limit)     {|line| block }   -> ARGF
 *     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 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
 */
static VALUE
argf_each_line(int argc, VALUE *argv, VALUE argf)
{
    RETURN_ENUMERATOR(argf, argc, argv);
    for (;;) {
    if (!next_argv()) return argf;
    rb_block_call(ARGF.current_file, rb_intern("each_line"), argc, argv, 0, 0);
    ARGF.next_p = 1;
    }
}

#eof?Boolean #eofBoolean

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

Overloads:

  • #eof?Boolean

    Returns:

    • (Boolean)
  • #eofBoolean

    Returns:

    • (Boolean)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.eof?  -> true or false
 *     ARGF.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
 */

static VALUE
argf_eof(VALUE argf)
{
    next_argv();
    if (RTEST(ARGF.current_file)) {
    if (ARGF.init_p == 0) return Qtrue;
    next_argv();
    ARGF_FORWARD(0, 0);
    if (rb_io_eof(ARGF.current_file)) {
        return Qtrue;
    }
    }
    return Qfalse;
}

#eof?Boolean #eofBoolean

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

Overloads:

  • #eof?Boolean

    Returns:

    • (Boolean)
  • #eofBoolean

    Returns:

    • (Boolean)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.eof?  -> true or false
 *     ARGF.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
 */

static VALUE
argf_eof(VALUE argf)
{
    next_argv();
    if (RTEST(ARGF.current_file)) {
    if (ARGF.init_p == 0) return Qtrue;
    next_argv();
    ARGF_FORWARD(0, 0);
    if (rb_io_eof(ARGF.current_file)) {
        return Qtrue;
    }
    }
    return Qfalse;
}

#external_encodingEncoding

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:

ARGF.external_encoding  #=>  #<Encoding:UTF-8>

Returns:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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:
 *
 *     ARGF.external_encoding  #=>  #<Encoding:UTF-8>
 *
 */
static VALUE
argf_external_encoding(VALUE argf)
{
    if (!RTEST(ARGF.current_file)) {
    return rb_enc_from_encoding(rb_default_external_encoding());
    }
    return rb_io_external_encoding(rb_io_check_io(ARGF.current_file));
}

#fileObject

Returns the current file as an IO or File object. #<IO:<STDIN>> is returned when the current file is STDIN.

For example:

$ echo "foo" > foo
$ echo "bar" > bar

$ ruby argf.rb foo bar

ARGF.file      #=> #<File:foo>
ARGF.read(5)   #=> "foo\nb"
ARGF.file      #=> #<File:bar>


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.file  -> IO or File object
 *
 *  Returns the current file as an +IO+ or +File+ object. #<IO:<STDIN>> is
 *  returned when the current file is STDIN.
 *
 *  For example:
 *
 *     $ echo "foo" > foo
 *     $ echo "bar" > bar
 *
 *     $ ruby argf.rb foo bar
 *
 *     ARGF.file      #=> #<File:foo>
 *     ARGF.read(5)   #=> "foo\nb"
 *     ARGF.file      #=> #<File:bar>
 */
static VALUE
argf_file(VALUE argf)
{
    next_argv();
    return ARGF.current_file;
}

#filenameObject #pathObject

Returns 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"


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.filename  -> String
 *     ARGF.path      -> String
 *
 *  Returns 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"
 */
static VALUE
argf_filename(VALUE argf)
{
    next_argv();
    return ARGF.filename;
}

#filenoFixnum #to_iFixnum

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

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.fileno    -> fixnum
 *     ARGF.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
 */
static VALUE
argf_fileno(VALUE argf)
{
    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream");
    }
    ARGF_FORWARD(0, 0);
    return rb_io_fileno(ARGF.current_file);
}

#getbytenil

Gets the next 8-bit byte (0..255) from ARGF. Returns nil if called at the end of the stream.

For example:

$ echo "foo" > file
$ ruby argf.rb file

ARGF.getbyte #=> 102
ARGF.getbyte #=> 111
ARGF.getbyte #=> 111
ARGF.getbyte #=> 10
ARGF.getbyte #=> nil

Returns:

  • (nil)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.getbyte  -> Fixnum or nil
 *
 *  Gets the next 8-bit byte (0..255) from +ARGF+. Returns +nil+ if called at
 *  the end of the stream.
 *
 *  For example:
 *
 *     $ echo "foo" > file
 *     $ ruby argf.rb file
 *
 *     ARGF.getbyte #=> 102
 *     ARGF.getbyte #=> 111
 *     ARGF.getbyte #=> 111
 *     ARGF.getbyte #=> 10
 *     ARGF.getbyte #=> nil
 */
static VALUE
argf_getbyte(VALUE argf)
{
    VALUE ch;

  retry:
    if (!next_argv()) return Qnil;
    if (TYPE(ARGF.current_file) != T_FILE) {
    ch = rb_funcall3(ARGF.current_file, rb_intern("getbyte"), 0, 0);
    }
    else {
    ch = rb_io_getbyte(ARGF.current_file);
    }
    if (NIL_P(ch) && ARGF.next_p != -1) {
    argf_close(ARGF.current_file);
    ARGF.next_p = 1;
    goto retry;
    }

    return ch;
}

#getcnil

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

Returns:

  • (nil)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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
 */
static VALUE
argf_getc(VALUE argf)
{
    VALUE ch;

  retry:
    if (!next_argv()) return Qnil;
    if (ARGF_GENERIC_INPUT_P()) {
    ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
    }
    else {
    ch = rb_io_getc(ARGF.current_file);
    }
    if (NIL_P(ch) && ARGF.next_p != -1) {
    argf_close(ARGF.current_file);
    ARGF.next_p = 1;
    goto retry;
    }

    return ch;
}

#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.

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.gets(sep=$/)     -> string
 *     ARGF.gets(limit)      -> string
 *     ARGF.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.
 *
 */
static VALUE
argf_gets(int argc, VALUE *argv, VALUE argf)
{
    VALUE line;

    line = argf_getline(argc, argv, argf);
    rb_lastline_set(line);

    return line;
}

#initialize_copyObject

:nodoc:



# File 'io.c'

/* :nodoc: */
static VALUE
argf_initialize_copy(VALUE argf, VALUE orig)
{
    ARGF = argf_of(orig);
    ARGF.argv = rb_obj_dup(ARGF.argv);
    if (ARGF.inplace) {
    const char *inplace = ARGF.inplace;
    ARGF.inplace = 0;
    ARGF.inplace = ruby_strdup(inplace);
    }
    return argf;
}

#inplace_modeObject

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.



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.inplace_mode  -> String
 *
 *  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.
 */
static VALUE
argf_inplace_mode_get(VALUE argf)
{
    if (!ARGF.inplace) return Qnil;
    return rb_str_new2(ARGF.inplace);
}

#inplace_mode=(ext) ⇒ Object

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.



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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_.
 */
static VALUE
argf_inplace_mode_set(VALUE argf, VALUE val)
{
    if (rb_safe_level() >= 1 && OBJ_TAINTED(val))
    rb_insecure_operation();

    if (!RTEST(val)) {
    if (ARGF.inplace) free(ARGF.inplace);
    ARGF.inplace = 0;
    }
    else {
    StringValue(val);
    if (ARGF.inplace) free(ARGF.inplace);
    ARGF.inplace = 0;
    ARGF.inplace = strdup(RSTRING_PTR(val));
    }
    return argf;
}

#internal_encodingEncoding

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 encoding was specified on the command-line, that value is used. If the encoding is unknown, nil is returned.

Returns:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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 encoding was
 *  specified on the command-line, that value is used. If the encoding is
 *  unknown, nil is returned.
 */
static VALUE
argf_internal_encoding(VALUE argf)
{
    if (!RTEST(ARGF.current_file)) {
    return rb_enc_from_encoding(rb_default_external_encoding());
    }
    return rb_io_internal_encoding(rb_io_check_io(ARGF.current_file));
}

#linenoInteger

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

Returns:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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
 */
static VALUE
argf_lineno(VALUE argf)
{
    return INT2FIX(ARGF.lineno);
}

#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

Returns:

  • (nil)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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
 */
static VALUE
argf_set_lineno(VALUE argf, VALUE val)
{
    ARGF.lineno = NUM2INT(val);
    ARGF.last_lineno = ARGF.lineno;
    return Qnil;
}

#each(sep = $/) {|line| ... } ⇒ Object #each(sep = $/, limit) {|line| ... } ⇒ Object #each(...) ⇒ Object

ARGF.each_line(sep=$/) {|line| block } -> ARGF

ARGF.each_line(sep=$/,limit) {|line| block }  -> ARGF
ARGF.each_line(...)                           -> an_enumerator

ARGF.lines(sep=$/)           {|line| block }   -> ARGF
ARGF.lines(sep=$/,limit)     {|line| block }   -> ARGF
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 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

Overloads:

  • #each(sep = $/) {|line| ... } ⇒ Object

    Yields:

    • (line)
  • #each(sep = $/, limit) {|line| ... } ⇒ Object

    Yields:

    • (line)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.each(sep=$/)            {|line| block }  -> ARGF
 *     ARGF.each(sep=$/,limit)      {|line| block }  -> ARGF
 *     ARGF.each(...)                                -> an_enumerator
 *
 *     ARGF.each_line(sep=$/)       {|line| block }  -> ARGF
 *     ARGF.each_line(sep=$/,limit) {|line| block }  -> ARGF
 *     ARGF.each_line(...)                           -> an_enumerator
 *
 *     ARGF.lines(sep=$/)           {|line| block }   -> ARGF
 *     ARGF.lines(sep=$/,limit)     {|line| block }   -> ARGF
 *     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 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
 */
static VALUE
argf_each_line(int argc, VALUE *argv, VALUE argf)
{
    RETURN_ENUMERATOR(argf, argc, argv);
    for (;;) {
    if (!next_argv()) return argf;
    rb_block_call(ARGF.current_file, rb_intern("each_line"), argc, argv, 0, 0);
    ARGF.next_p = 1;
    }
}

#filenameObject #pathObject

Returns 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"


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.filename  -> String
 *     ARGF.path      -> String
 *
 *  Returns 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"
 */
static VALUE
argf_filename(VALUE argf)
{
    next_argv();
    return ARGF.filename;
}

#tellInteger #posInteger

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

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.tell  -> Integer
 *     ARGF.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
 *
 */
static VALUE
argf_tell(VALUE argf)
{
    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream to tell");
    }
    ARGF_FORWARD(0, 0);
    return rb_io_tell(ARGF.current_file);
}

#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"

Returns:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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"
 */
static VALUE
argf_set_pos(VALUE argf, VALUE offset)
{
    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream to set position");
    }
    ARGF_FORWARD(1, &offset);
    return rb_io_set_pos(ARGF.current_file, offset);
}

#read([length [, buffer]]) ⇒ String?

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"
ARGF.read(200) #=> "small\nlarge"
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.

Returns:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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"
 *     ARGF.read(200) #=> "small\nlarge"
 *     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+.
 */

static VALUE
argf_read(int argc, VALUE *argv, VALUE argf)
{
    VALUE tmp, str, length;
    long len = 0;

    rb_scan_args(argc, argv, "02", &length, &str);
    if (!NIL_P(length)) {
    len = NUM2LONG(argv[0]);
    }
    if (!NIL_P(str)) {
    StringValue(str);
    rb_str_resize(str,0);
    argv[1] = Qnil;
    }

  retry:
    if (!next_argv()) {
    return str;
    }
    if (ARGF_GENERIC_INPUT_P()) {
    tmp = argf_forward(argc, argv, argf);
    }
    else {
    tmp = io_read(argc, argv, ARGF.current_file);
    }
    if (NIL_P(str)) str = tmp;
    else if (!NIL_P(tmp)) rb_str_append(str, tmp);
    if (NIL_P(tmp) || NIL_P(length)) {
    if (ARGF.next_p != -1) {
        argf_close(ARGF.current_file);
        ARGF.next_p = 1;
        goto retry;
    }
    }
    else if (argc >= 1) {
    if (RSTRING_LEN(str) < len) {
        len -= RSTRING_LEN(str);
        argv[0] = INT2NUM(len);
        goto retry;
    }
    }
    return str;
}

#readbyteObject

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)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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)
 */
static VALUE
argf_readbyte(VALUE argf)
{
    VALUE c;

    NEXT_ARGF_FORWARD(0, 0);
    c = argf_getbyte(argf);
    if (NIL_P(c)) {
    rb_eof_error();
    }
    return c;
}

#readcharnil

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"
ARGF.readchar  #=> "o"
ARGF.readchar  #=> "\n"
ARGF.readchar  #=> end of file reached (EOFError)

Returns:

  • (nil)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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"
 *     ARGF.readchar  #=> "o"
 *     ARGF.readchar  #=> "\n"
 *     ARGF.readchar  #=> end of file reached (EOFError)
 */
static VALUE
argf_readchar(VALUE argf)
{
    VALUE ch;

  retry:
    if (!next_argv()) rb_eof_error();
    if (TYPE(ARGF.current_file) != T_FILE) {
    ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
    }
    else {
    ch = rb_io_getc(ARGF.current_file);
    }
    if (NIL_P(ch) && ARGF.next_p != -1) {
    argf_close(ARGF.current_file);
    ARGF.next_p = 1;
    goto retry;
    }

    return ch;
}

#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.

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.readline(sep=$/)     -> string
 *     ARGF.readline(limit)      -> string
 *     ARGF.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.
 */
static VALUE
argf_readline(int argc, VALUE *argv, VALUE argf)
{
    VALUE line;

    if (!next_argv()) rb_eof_error();
    ARGF_FORWARD(argc, argv);
    line = argf_gets(argc, argv, argf);
    if (NIL_P(line)) {
    rb_eof_error();
    }

    return line;
}

#readlines(sep = $/) ⇒ Array #readlines(limit) ⇒ Array #readlines(sep, limit) ⇒ Array

ARGF.to_a(sep=$/) -> array

ARGF.to_a(limit)      -> array
ARGF.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"

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.readlines(sep=$/)     -> array
 *     ARGF.readlines(limit)      -> array
 *     ARGF.readlines(sep, limit) -> array
 *
 *     ARGF.to_a(sep=$/)     -> array
 *     ARGF.to_a(limit)      -> array
 *     ARGF.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"
 */
static VALUE
argf_readlines(int argc, VALUE *argv, VALUE argf)
{
    VALUE line, ary;

    ary = rb_ary_new();
    while (!NIL_P(line = argf_getline(argc, argv, argf))) {
    rb_ary_push(ary, line);
    }

    return ary;
}

#readpartial(maxlen) ⇒ String #readpartial(maxlen, outbuf) ⇒ Object

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.

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.

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.readpartial(maxlen)              -> string
 *     ARGF.readpartial(maxlen, outbuf)      -> outbuf
 *
 *  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 <code>EOFError</code> 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.
 */

static VALUE
argf_readpartial(int argc, VALUE *argv, VALUE argf)
{
    VALUE tmp, str, length;

    rb_scan_args(argc, argv, "11", &length, &str);
    if (!NIL_P(str)) {
        StringValue(str);
        argv[1] = str;
    }

    if (!next_argv()) {
        rb_str_resize(str, 0);
        rb_eof_error();
    }
    if (ARGF_GENERIC_INPUT_P()) {
    struct argf_call_arg arg;
    arg.argc = argc;
    arg.argv = argv;
    arg.argf = argf;
    tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
             RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
    }
    else {
        tmp = io_getpartial(argc, argv, ARGF.current_file, 0);
    }
    if (NIL_P(tmp)) {
        if (ARGF.next_p == -1) {
            rb_eof_error();
        }
        argf_close(ARGF.current_file);
        ARGF.next_p = 1;
        if (RARRAY_LEN(ARGF.argv) == 0)
            rb_eof_error();
        if (NIL_P(str))
            str = rb_str_new(NULL, 0);
        return str;
    }
    return tmp;
}

#rewind0

Positions the current file to the beginning of input, resetting ARGF.lineno to zero.

ARGF.readline   #=> "This is line one\n"
ARGF.rewind     #=> 0
ARGF.lineno     #=> 0
ARGF.readline   #=> "This is line one\n"

Returns:

  • (0)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.rewind   -> 0
 *
 *  Positions the current file to the beginning of input, resetting
 *  +ARGF.lineno+ to zero.
 *
 *     ARGF.readline   #=> "This is line one\n"
 *     ARGF.rewind     #=> 0
 *     ARGF.lineno     #=> 0
 *     ARGF.readline   #=> "This is line one\n"
 */
static VALUE
argf_rewind(VALUE argf)
{
    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream to rewind");
    }
    ARGF_FORWARD(0, 0);
    return rb_io_rewind(ARGF.current_file);
}

#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.

Returns:

  • (0)


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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.
 */
static VALUE
argf_seek_m(int argc, VALUE *argv, VALUE argf)
{
    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream to seek");
    }
    ARGF_FORWARD(argc, argv);
    return rb_io_seek_m(argc, argv, ARGF.current_file);
}

#set_encoding(ext_enc) ⇒ Object #set_encoding("ext_enc:int_enc") ⇒ Object #set_encoding(ext_enc, int_enc) ⇒ Object #set_encoding("ext_enc:int_enc", opt) ⇒ Object #set_encoding(ext_enc, int_enc, opt) ⇒ Object

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.

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.

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.


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.set_encoding(ext_enc)                -> ARGF
 *     ARGF.set_encoding("ext_enc:int_enc")      -> ARGF
 *     ARGF.set_encoding(ext_enc, int_enc)       -> ARGF
 *     ARGF.set_encoding("ext_enc:int_enc", opt) -> ARGF
 *     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.
 *
 *  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.
 *
 *  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.
 */
static VALUE
argf_set_encoding(int argc, VALUE *argv, VALUE argf)
{
    rb_io_t *fptr;

    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream to set encoding");
    }
    rb_io_set_encoding(argc, argv, ARGF.current_file);
    GetOpenFile(ARGF.current_file, fptr);
    ARGF.encs = fptr->encs;
    return argf;
}

#skipObject

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"


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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"
 */
static VALUE
argf_skip(VALUE argf)
{
    if (ARGF.init_p && ARGF.next_p == 0) {
    argf_close(ARGF.current_file);
    ARGF.next_p = 1;
    }
    return argf;
}

#tellInteger #posInteger

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

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.tell  -> Integer
 *     ARGF.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
 *
 */
static VALUE
argf_tell(VALUE argf)
{
    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream to tell");
    }
    ARGF_FORWARD(0, 0);
    return rb_io_tell(ARGF.current_file);
}

#readlines(sep = $/) ⇒ Array #readlines(limit) ⇒ Array #readlines(sep, limit) ⇒ Array

ARGF.to_a(sep=$/) -> array

ARGF.to_a(limit)      -> array
ARGF.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"

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.readlines(sep=$/)     -> array
 *     ARGF.readlines(limit)      -> array
 *     ARGF.readlines(sep, limit) -> array
 *
 *     ARGF.to_a(sep=$/)     -> array
 *     ARGF.to_a(limit)      -> array
 *     ARGF.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"
 */
static VALUE
argf_readlines(int argc, VALUE *argv, VALUE argf)
{
    VALUE line, ary;

    ary = rb_ary_new();
    while (!NIL_P(line = argf_getline(argc, argv, argf))) {
    rb_ary_push(ary, line);
    }

    return ary;
}

#filenoFixnum #to_iFixnum

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

Overloads:



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.fileno    -> fixnum
 *     ARGF.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
 */
static VALUE
argf_fileno(VALUE argf)
{
    if (!next_argv()) {
    rb_raise(rb_eArgError, "no stream");
    }
    ARGF_FORWARD(0, 0);
    return rb_io_fileno(ARGF.current_file);
}

#to_ioObject

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>>


# File 'io.c'

/*
 *  call-seq:
 *     ARGF.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>>
 */
static VALUE
argf_to_io(VALUE argf)
{
    next_argv();
    ARGF_FORWARD(0, 0);
    return ARGF.current_file;
}

#to_sObject

Returns "ARGF".



# File 'io.c'

/*
 *  call-seq:
 *     ARGF.to_s  -> String
 *
 *  Returns "ARGF".
 */
static VALUE
argf_to_s(VALUE argf)
{
    return rb_str_new2("ARGF");
}