class ARGF
ARGF andARGV¶↑
The ARGF object works with the array at global variableARGV to make$stdin and file streams available in the Ruby program:
ARGV may be thought of as theargument vector array.
Initially, it contains the command-line arguments and options that are passed to the Ruby program; the program can modify that array as it likes.
ARGF may be thought of as theargument files object.
It can access file streams and/or the
$stdinstream, based on what it finds inARGV. This provides a convenient way for the command line to specify streams for a Ruby program to read.
Reading¶↑
ARGF may read fromsource streams, which at any particular time are determined by the content ofARGV.
Simplest Case¶↑
When thevery first ARGF read occurs with an emptyARGV ([]), the source is$stdin:
File
t.rb:p ['ARGV',ARGV]p ['ARGF.read',ARGF.read]
Commands and outputs (see below for the content of files
foo.txtandbar.txt):$ echo "Open the pod bay doors, Hal." | ruby t.rb["ARGV", []]["ARGF.read", "Open the pod bay doors, Hal.\n"]$ cat foo.txt bar.txt | ruby t.rb["ARGV", []]["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"]
About the Examples¶↑
Many examples here assume the existence of filesfoo.txt andbar.txt:
$ cat foo.txtFoo 0Foo 1$ cat bar.txtBar 0Bar 1Bar 2Bar 3
Sources inARGV¶↑
For any ARGF readexcept thesimplest case (that is,except for thevery first ARGF read with an emptyARGV), the sources are found inARGV.
ARGF assumes that each element in arrayARGV is a potential source, and is one of:
The string path to a file that may be opened as a stream.
The character
'-', meaning stream$stdin.
Each element that isnot one of these should be removed fromARGV before ARGF accesses that source.
In the following example:
Filepaths
foo.txtandbar.txtmay be retained as potential sources.Options
--xyzzyand--mojoshould be removed.
Example:
File
t.rb:# Print arguments (and options, if any) found on command line.p ['ARGV',ARGV]
Command and output:
$ ruby t.rb --xyzzy --mojo foo.txt bar.txt["ARGV", ["--xyzzy", "--mojo", "foo.txt", "bar.txt"]]
ARGF’s stream access considers the elements ofARGV, left to right:
File
t.rb:p"ARGV: #{ARGV}"p"Read: #{ARGF.read}"# Read everything from all specified streams.
Command and output:
$ ruby t.rb foo.txt bar.txt"ARGV: [\"foo.txt\", \"bar.txt\"]""Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"
Because the value atARGV is an ordinary array, you can manipulate it to control which sources ARGF considers:
If you remove an element from
ARGV, ARGF will not consider the corresponding source.If you add an element to
ARGV, ARGF will consider the corresponding source.
Each element inARGV is removed when its corresponding source is accessed; when all sources have been accessed, the array is empty:
File
t.rb:untilARGV.empty?&&ARGF.eof?p"ARGV: #{ARGV}"p"Line: #{ARGF.readline}"# Read each line from each specified stream.end
Command and output:
$ ruby t.rb foo.txt bar.txt"ARGV: [\"foo.txt\", \"bar.txt\"]""Line: Foo 0\n""ARGV: [\"bar.txt\"]""Line: Foo 1\n""ARGV: [\"bar.txt\"]""Line: Bar 0\n""ARGV: []""Line: Bar 1\n""ARGV: []""Line: Bar 2\n""ARGV: []""Line: Bar 3\n"
Filepaths inARGV¶↑
TheARGV array may contain filepaths the specify sources for ARGF reading.
This program prints what it reads from files at the paths specified on the command line:
File
t.rb:p ['ARGV',ARGV]# Read and print all content from the specified sources.p ['ARGF.read',ARGF.read]
Command and output:
$ ruby t.rb foo.txt bar.txt["ARGV", [foo.txt, bar.txt]["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"]
Specifying$stdin inARGV¶↑
To specify stream$stdin inARGV, us the character'-':
File
t.rb:p ['ARGV',ARGV]p ['ARGF.read',ARGF.read]
Command and output:
$ echo "Open the pod bay doors, Hal." | ruby t.rb -["ARGV", ["-"]]["ARGF.read", "Open the pod bay doors, Hal.\n"]
When no character'-' is given, stream$stdin is ignored (exception: seeSpecifying $stdin in ARGV):
Command and output:
$ echo "Open the pod bay doors, Hal." | ruby t.rb foo.txt bar.txt"ARGV: [\"foo.txt\", \"bar.txt\"]""Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"
Mixtures and Repetitions inARGV¶↑
For an ARGF reader,ARGV may contain any mixture of filepaths and character'-', including repetitions.
Modifications toARGV¶↑
The running Ruby program may make any modifications to theARGV array; the current value ofARGV affects ARGF reading.
EmptyARGV¶↑
For an emptyARGV, an ARGF read method either returnsnil or raises an exception, depending on the specific method.
More Read Methods¶↑
As seen above, methodARGF#read reads the content of all sources into a single string. Other ARGF methods provide other ways to access that content; these include:
Codepoint access:
each_codepoint.Source access:
read,read_nonblock,readpartial.
About Enumerable¶↑
ARGF includes moduleEnumerable. Virtually all methods in Enumerable call methodeach in the including class.
Note well: In ARGF, methodeach returns data from thesources,not fromARGV; therefore, for example,ARGF#entries returns an array of lines from the sources, not an array of the strings fromARGV:
File
t.rb:p ['ARGV',ARGV]p ['ARGF.entries',ARGF.entries]
Command and output:
$ ruby t.rb foo.txt bar.txt["ARGV", ["foo.txt", "bar.txt"]]["ARGF.entries", ["Foo 0\n", "Foo 1\n", "Bar 0\n", "Bar 1\n", "Bar 2\n", "Bar 3\n"]]
Writing¶↑
Ifinplace mode is in effect, ARGF may write to target streams, which at any particular time are determined by the content of ARGV.
Methods about inplace mode:
Methods for writing:
Public Instance Methods
Source
static VALUEargf_argv(VALUE argf){ return ARGF.argv;}Returns theARGV array, which contains the arguments passed to your script, one per element.
For example:
$ ruby argf.rb -v glark.txtARGF.argv #=> ["-v", "glark.txt"]
Source
static VALUEargf_binmode_m(VALUE argf){ ARGF.binmode = 1; next_argv(); ARGF_FORWARD(0, 0); rb_io_ascii8bit_binmode(ARGF.current_file); return argf;}PutsARGF 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.
Encodingconversion is disabled.Content is treated as ASCII-8BIT.
Source
static VALUEargf_binmode_p(VALUE argf){ return RBOOL(ARGF.binmode);}Returns true ifARGF is being read in binary mode; false otherwise. To enable binary mode useARGF.binmode.
For example:
ARGF.binmode?#=> falseARGF.binmodeARGF.binmode?#=> true
Source
static VALUEargf_close_m(VALUE argf){ next_argv(); argf_close(argf); if (ARGF.next_p != -1) { ARGF.next_p = 1; } ARGF.lineno = 0; return argf;}Closes the current file and skips to the next file in ARGV. If there are no more files to open, just closes the current file. STDIN will not be closed.
For example:
$ ruby argf.rb foo barARGF.filename #=> "foo"ARGF.closeARGF.filename #=> "bar"ARGF.close
Source
static VALUEargf_closed(VALUE argf){ next_argv(); ARGF_FORWARD(0, 0); return rb_io_closed_p(ARGF.current_file);}Returnstrue if the current file has been closed;false otherwise. UseARGF.close to actually close the current file.
Source
static VALUEargf_each_line(int argc, VALUE *argv, VALUE argf){ RETURN_ENUMERATOR(argf, argc, argv); FOREACH_ARGF() { argf_block_call_line(rb_intern("each_line"), argc, argv, argf); } return argf;}Returns an enumerator which iterates over each line (separated bysep, which defaults to your platform’s newline character) of each file inARGV. If a block is supplied, each line in turn will be yielded to the block, otherwise an enumerator is returned. The optionallimit argument is anInteger 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. TheARGF.filename andARGF.lineno methods can be used to determine the filename of the current line and line number of the whole input, respectively.
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.each_linedo|line|putsARGF.filenameifARGF.file.lineno==1puts"#{ARGF.file.lineno}: #{line}"end
While the following code prints only the first file’s name at first, and the contents with line number counted through all named files.
ARGF.each_linedo|line|putsARGF.filenameifARGF.lineno==1puts"#{ARGF.lineno}: #{line}"end
Source
static VALUEargf_each_byte(VALUE argf){ RETURN_ENUMERATOR(argf, 0, 0); FOREACH_ARGF() { argf_block_call(rb_intern("each_byte"), 0, 0, argf); } return argf;}Iterates over each byte of each file inARGV. A byte is returned as anInteger 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. TheARGF.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]
Source
static VALUEargf_each_char(VALUE argf){ RETURN_ENUMERATOR(argf, 0, 0); FOREACH_ARGF() { argf_block_call(rb_intern("each_char"), 0, 0, argf); } return argf;}Iterates over each character of each file inARGF.
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. TheARGF.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.
Source
static VALUEargf_each_codepoint(VALUE argf){ RETURN_ENUMERATOR(argf, 0, 0); FOREACH_ARGF() { argf_block_call(rb_intern("each_codepoint"), 0, 0, argf); } return argf;}Iterates over each codepoint of each file inARGF.
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. TheARGF.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.
Returns an enumerator which iterates over each line (separated bysep, which defaults to your platform’s newline character) of each file inARGV. If a block is supplied, each line in turn will be yielded to the block, otherwise an enumerator is returned. The optionallimit argument is anInteger 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. TheARGF.filename andARGF.lineno methods can be used to determine the filename of the current line and line number of the whole input, respectively.
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.each_linedo|line|putsARGF.filenameifARGF.file.lineno==1puts"#{ARGF.file.lineno}: #{line}"end
While the following code prints only the first file’s name at first, and the contents with line number counted through all named files.
ARGF.each_linedo|line|putsARGF.filenameifARGF.lineno==1puts"#{ARGF.lineno}: #{line}"end
Source
static VALUEargf_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;}Returns true if the current file inARGF is at end of file, i.e. it has no data to read. The stream must be opened for reading or anIOError will be raised.
$ echo "eof" | ruby argf.rbARGF.eof? #=> false3.times { ARGF.readchar }ARGF.eof? #=> falseARGF.readchar #=> "\n"ARGF.eof? #=> trueReturns true if the current file inARGF is at end of file, i.e. it has no data to read. The stream must be opened for reading or anIOError will be raised.
$ echo "eof" | ruby argf.rbARGF.eof? #=> false3.times { ARGF.readchar }ARGF.eof? #=> falseARGF.readchar #=> "\n"ARGF.eof? #=> trueSource
static VALUEargf_external_encoding(VALUE argf){ return argf_encoding(argf, rb_io_external_encoding);}Returns the external encoding for files read fromARGF as anEncoding object. The external encoding is the encoding of the text as stored in a file. Contrast withARGF.internal_encoding, which is the encoding used to represent this text within Ruby.
To set the external encoding useARGF.set_encoding.
For example:
ARGF.external_encoding#=> #<Encoding:UTF-8>
Source
static VALUEargf_file(VALUE argf){ next_argv(); return ARGF.current_file;}Returns the current file as anIO orFile object.$stdin is returned when the current file is STDIN.
For example:
$ echo "foo" > foo$ echo "bar" > bar$ ruby argf.rb foo barARGF.file #=> #<File:foo>ARGF.read(5) #=> "foo\nb"ARGF.file #=> #<File:bar>
Source
static VALUEargf_filename(VALUE argf){ next_argv(); return ARGF.filename;}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 glarkARGF.filename #=> "foo"ARGF.read(5) #=> "foo\nb"ARGF.filename #=> "bar"ARGF.skipARGF.filename #=> "glark"
Source
static VALUEargf_fileno(VALUE argf){ if (!next_argv()) { rb_raise(rb_eArgError, "no stream"); } ARGF_FORWARD(0, 0); return rb_io_fileno(ARGF.current_file);}Returns an integer representing the numeric file descriptor for the current file. Raises anArgumentError if there isn’t a current file.
ARGF.fileno#=> 3
Source
static VALUEargf_getbyte(VALUE argf){ VALUE ch; retry: if (!next_argv()) return Qnil; if (!RB_TYPE_P(ARGF.current_file, T_FILE)) { ch = forward_current(rb_intern("getbyte"), 0, 0); } else { ch = rb_io_getbyte(ARGF.current_file); } if (NIL_P(ch) && ARGF.next_p != -1) { argf_close(argf); ARGF.next_p = 1; goto retry; } return ch;}Gets the next 8-bit byte (0..255) fromARGF. Returnsnil if called at the end of the stream.
For example:
$ echo "foo" > file$ ruby argf.rb fileARGF.getbyte #=> 102ARGF.getbyte #=> 111ARGF.getbyte #=> 111ARGF.getbyte #=> 10ARGF.getbyte #=> nil
Source
static VALUEargf_getc(VALUE argf){ VALUE ch; retry: if (!next_argv()) return Qnil; if (ARGF_GENERIC_INPUT_P()) { ch = forward_current(rb_intern("getc"), 0, 0); } else { ch = rb_io_getc(ARGF.current_file); } if (NIL_P(ch) && ARGF.next_p != -1) { argf_close(argf); ARGF.next_p = 1; goto retry; } return ch;}Reads the next character fromARGF and returns it as aString. Returnsnil 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 fileARGF.getc #=> "f"ARGF.getc #=> "o"ARGF.getc #=> "o"ARGF.getc #=> "\n"ARGF.getc #=> nilARGF.getc #=> nil
Source
static VALUEargf_gets(int argc, VALUE *argv, VALUE argf){ VALUE line; line = argf_getline(argc, argv, argf); rb_lastline_set(line); return line;}Returns the next line from the current file inARGF.
By default lines are assumed to be separated by$/; to use a different character as a separator, supply it as aString for thesep argument.
The optionallimit argument specifies how many characters of each line to return. By default all characters are returned.
SeeIO.readlines for details about getline_args.
Source
static VALUEargf_inplace_mode_get(VALUE argf){ if (!ARGF.inplace) return Qnil; if (NIL_P(ARGF.inplace)) return rb_str_new(0, 0); return rb_str_dup(ARGF.inplace);}Returns the file extension appended to the names of backup copies of modified files under in-place edit mode. This value can be set usingARGF.inplace_mode= or passing the-i switch to the Ruby binary.
Source
static VALUEargf_inplace_mode_set(VALUE argf, VALUE val){ if (!RTEST(val)) { ARGF.inplace = Qfalse; } else if (StringValueCStr(val), !RSTRING_LEN(val)) { ARGF.inplace = Qnil; } else { ARGF.inplace = rb_str_new_frozen(val); } return argf;}Sets the filename extension for in-place editing mode to the givenString. The backup copy of each file being edited has this value appended to its filename.
For example:
$ ruby argf.rb file.txtARGF.inplace_mode = '.bak'ARGF.each_line do |line| print line.sub("foo","bar")endFirst,file.txt.bak is created as a backup copy offile.txt. Then, each line offile.txt has the first occurrence of “foo” replaced with “bar”.
Source
static VALUEargf_internal_encoding(VALUE argf){ return argf_encoding(argf, rb_io_internal_encoding);}Returns the internal encoding for strings read fromARGF as anEncoding object.
IfARGF.set_encoding has been called with two encoding names, the second is returned. Otherwise, ifEncoding.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.
Source
static VALUEargf_lineno(VALUE argf){ return INT2FIX(ARGF.lineno);}Returns the current line number ofARGF as a whole. This value can be set manually withARGF.lineno=.
For example:
ARGF.lineno#=> 0ARGF.readline#=> "This is line 1\n"ARGF.lineno#=> 1
Source
static VALUEargf_set_lineno(VALUE argf, VALUE val){ ARGF.lineno = NUM2INT(val); ARGF.last_lineno = ARGF.lineno; return val;}Sets the line number ofARGF as a whole to the givenInteger.
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 useARGF.lineno.
For example:
ARGF.lineno#=> 0ARGF.readline#=> "This is line 1\n"ARGF.lineno#=> 1ARGF.lineno =0#=> 0ARGF.lineno#=> 0
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 glarkARGF.filename #=> "foo"ARGF.read(5) #=> "foo\nb"ARGF.filename #=> "bar"ARGF.skipARGF.filename #=> "glark"
Returns the current offset (in bytes) of the current file inARGF.
ARGF.pos#=> 0ARGF.gets#=> "This is line one\n"ARGF.pos#=> 17
Source
static VALUEargf_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);}Seeks to the position given byposition (in bytes) inARGF.
For example:
ARGF.pos =17ARGF.gets#=> "This is line two\n"
Source
VALUErb_io_print(int argc, const VALUE *argv, VALUE out){ int i; VALUE line; /* if no argument given, print `$_' */ if (argc == 0) { argc = 1; line = rb_lastline_get(); argv = &line; } if (argc > 1 && !NIL_P(rb_output_fs)) { rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$, is set to non-nil value"); } for (i=0; i<argc; i++) { if (!NIL_P(rb_output_fs) && i>0) { rb_io_write(out, rb_output_fs); } rb_io_write(out, argv[i]); } if (argc > 0 && !NIL_P(rb_output_rs)) { rb_io_write(out, rb_output_rs); } return Qnil;}Writes the given objects to the stream; returnsnil. Appends the output record separator$OUTPUT_RECORD_SEPARATOR ($\), if it is notnil. SeeLine IO.
With argumentobjects given, for each object:
Converts via its method
to_sif not a string.Writes to the stream.
If not the last object, writes the output field separator
$OUTPUT_FIELD_SEPARATOR($,) if it is notnil.
With default separators:
f =File.open('t.tmp','w+')objects = [0,0.0,Rational(0,1),Complex(0,0),:zero,'zero']p$OUTPUT_RECORD_SEPARATORp$OUTPUT_FIELD_SEPARATORf.print(*objects)f.rewindpf.readf.close
Output:
nilnil"00.00/10+0izerozero"
With specified separators:
$\ ="\n"$, =','f.rewindf.print(*objects)f.rewindpf.read
Output:
"0,0.0,0/1,0+0i,zero,zero\n"With no argument given, writes the content of$_ (which is usually the most recent user input):
f =File.open('t.tmp','w+')gets# Sets $_ to the most recent user input.f.printf.close
Source
VALUErb_io_printf(int argc, const VALUE *argv, VALUE out){ rb_io_write(out, rb_f_sprintf(argc, argv)); return Qnil;}Formats and writesobjects to the stream.
For details onformat_string, seeFormat Specifications.
Source
static VALUErb_io_putc(VALUE io, VALUE ch){ VALUE str; if (RB_TYPE_P(ch, T_STRING)) { str = rb_str_substr(ch, 0, 1); } else { char c = NUM2CHR(ch); str = rb_str_new(&c, 1); } rb_io_write(io, str); return ch;}Writes a character to the stream. SeeCharacter IO.
Ifobject is numeric, converts to integer if necessary, then writes the character whose code is the least significant byte; ifobject is a string, writes the first character:
$stdout.putc"A"$stdout.putc65
Output:
AASource
VALUErb_io_puts(int argc, const VALUE *argv, VALUE out){ VALUE line, args[2]; /* if no argument given, print newline. */ if (argc == 0) { rb_io_write(out, rb_default_rs); return Qnil; } for (int i = 0; i < argc; i++) { // Convert the argument to a string: if (RB_TYPE_P(argv[i], T_STRING)) { line = argv[i]; } else if (rb_exec_recursive(io_puts_ary, argv[i], out)) { continue; } else { line = rb_obj_as_string(argv[i]); } // Write the line: int n = 0; if (RSTRING_LEN(line) == 0) { args[n++] = rb_default_rs; } else { args[n++] = line; if (!rb_str_end_with_asciichar(line, '\n')) { args[n++] = rb_default_rs; } } rb_io_writev(out, n, args); } return Qnil;}Writes the givenobjects to the stream, which must be open for writing; returnsnil.\ Writes a newline after each that does not already end with a newline sequence. If called without arguments, writes a newline. SeeLine IO.
Note that each added newline is the character"\n"<//tt>, not the output record separator (<tt>$\).
Treatment for each object:
String: writes the string.
Neither string nor array: writes
object.to_s.Array: writes each element of the array; arrays may be nested.
To keep these examples brief, we define this helper method:
defshow(*objects)# Puts objects to file.f =File.new('t.tmp','w+')f.puts(objects)# Return file content.f.rewindpf.readf.closeend# Strings without newlines.show('foo','bar','baz')# => "foo\nbar\nbaz\n"# Strings, some with newlines.show("foo\n",'bar',"baz\n")# => "foo\nbar\nbaz\n"# Neither strings nor arrays:show(0,0.0,Rational(0,1),Complex(9,0),:zero)# => "0\n0.0\n0/1\n9+0i\nzero\n"# Array of strings.show(['foo',"bar\n",'baz'])# => "foo\nbar\nbaz\n"# Nested arrays.show([[[0,1],2,3],4,5])# => "0\n1\n2\n3\n4\n5\n"
Source
static VALUEargf_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); ARGF.next_p = 1; goto retry; } } else if (argc >= 1) { long slen = RSTRING_LEN(str); if (slen < len) { argv[0] = LONG2NUM(len - slen); goto retry; } } return str;}Readslength bytes fromARGF. 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 ornil.
Iflength is a positive integer,read tries to readlength bytes without any conversion (binary mode). It returnsnil if an EOF is encountered before anything can be read. Fewer thanlength bytes are returned if an EOF is encountered during the read. In the case of an integerlength, the resulting string is always in ASCII-8BIT encoding.
Iflength is omitted or isnil, it reads until EOF and the encoding conversion is applied, if applicable. A string is returned even if EOF is encountered before any data is read.
Iflength is zero, it returns an empty string ("").
If the optionaloutbuf argument is present, it must reference aString, which will receive the data. Theoutbuf will contain only the received data after the method call even if it is not empty at the beginning.
For example:
$ echo "small" > small.txt$ echo "large" > large.txt$ ./glark.rb small.txt large.txtARGF.read #=> "small\nlarge"ARGF.read(200) #=> "small\nlarge"ARGF.read(2) #=> "sm"ARGF.read(0) #=> ""
Note that this method behaves like the fread() function in C. This means it retries to invoke read(2) system calls to read data with the specified length. If you need the behavior like a single read(2) system call, considerARGF#readpartial orARGF#read_nonblock.
Source
static VALUEargf_read_nonblock(int argc, VALUE *argv, VALUE argf){ VALUE opts; rb_scan_args(argc, argv, "11:", NULL, NULL, &opts); if (!NIL_P(opts)) argc--; return argf_getpartial(argc, argv, argf, opts, 1);}Reads at mostmaxlen bytes from theARGF stream in non-blocking mode.
Source
static VALUEargf_readbyte(VALUE argf){ VALUE c; NEXT_ARGF_FORWARD(0, 0); c = argf_getbyte(argf); if (NIL_P(c)) { rb_eof_error(); } return c;}Reads the next 8-bit byte fromARGF and returns it as anInteger. Raises anEOFError after the last byte of the last file has been read.
For example:
$ echo "foo" > file$ ruby argf.rb fileARGF.readbyte #=> 102ARGF.readbyte #=> 111ARGF.readbyte #=> 111ARGF.readbyte #=> 10ARGF.readbyte #=> end of file reached (EOFError)
Source
static VALUEargf_readchar(VALUE argf){ VALUE ch; retry: if (!next_argv()) rb_eof_error(); if (!RB_TYPE_P(ARGF.current_file, T_FILE)) { ch = forward_current(rb_intern("getc"), 0, 0); } else { ch = rb_io_getc(ARGF.current_file); } if (NIL_P(ch) && ARGF.next_p != -1) { argf_close(argf); ARGF.next_p = 1; goto retry; } return ch;}Reads the next character fromARGF and returns it as aString. Raises anEOFError after the last character of the last file has been read.
For example:
$ echo "foo" > file$ ruby argf.rb fileARGF.readchar #=> "f"ARGF.readchar #=> "o"ARGF.readchar #=> "o"ARGF.readchar #=> "\n"ARGF.readchar #=> end of file reached (EOFError)
Source
static VALUEargf_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;}Returns the next line from the current file inARGF.
By default lines are assumed to be separated by$/; to use a different character as a separator, supply it as aString for thesep argument.
The optionallimit argument specifies how many characters of each line to return. By default all characters are returned.
AnEOFError is raised at the end of the file.
Source
static VALUEargf_readlines(int argc, VALUE *argv, VALUE argf){ long lineno = ARGF.lineno; VALUE lines, ary; ary = rb_ary_new(); while (next_argv()) { if (ARGF_GENERIC_INPUT_P()) { lines = forward_current(rb_intern("readlines"), argc, argv); } else { lines = rb_io_readlines(argc, argv, ARGF.current_file); argf_close(argf); } ARGF.next_p = 1; rb_ary_concat(ary, lines); ARGF.lineno = lineno + RARRAY_LEN(ary); ARGF.last_lineno = ARGF.lineno; } ARGF.init_p = 0; return ary;}Reads each file inARGF in its entirety, returning anArray containing lines from the files. Lines are assumed to be separated bysep.
lines =ARGF.readlineslines[0]#=> "This is line one\n"
SeeIO.readlines for a full description of all options.
Source
static VALUEargf_readpartial(int argc, VALUE *argv, VALUE argf){ return argf_getpartial(argc, argv, argf, Qnil, 0);}Reads at mostmaxlen bytes from theARGF stream.
If the optionaloutbuf argument is present, it must reference aString, which will receive the data. Theoutbuf will contain only the received data after the method call even if it is not empty at the beginning.
It raisesEOFError on end ofARGF stream. SinceARGF stream is a concatenation of multiple files, internally EOF is occur for each file.ARGF.readpartial returns empty strings for EOFs except the last one and raisesEOFError for the last one.
Source
static VALUEargf_rewind(VALUE argf){ VALUE ret; int old_lineno; if (!next_argv()) { rb_raise(rb_eArgError, "no stream to rewind"); } ARGF_FORWARD(0, 0); old_lineno = RFILE(ARGF.current_file)->fptr->lineno; ret = rb_io_rewind(ARGF.current_file); if (!global_argf_p(argf)) { ARGF.last_lineno = ARGF.lineno -= old_lineno; } return ret;}Positions the current file to the beginning of input, resettingARGF.lineno to zero.
ARGF.readline#=> "This is line one\n"ARGF.rewind#=> 0ARGF.lineno#=> 0ARGF.readline#=> "This is line one\n"
Source
static VALUEargf_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);}Seeks to offsetamount (anInteger) in theARGF stream according to the value ofwhence. SeeIO#seek for further details.
Source
static VALUEargf_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;}If single argument is specified, strings read fromARGF 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 optionalHash argument can be used to adjust the conversion process. The structure of this hash is explained in theString#encode documentation.
For example:
ARGF.set_encoding('ascii')# Tag the input as US-ASCII textARGF.set_encoding(Encoding::UTF_8)# Tag the input as UTF-8 textARGF.set_encoding('utf-8','ascii')# Transcode the input from US-ASCII# to UTF-8.
Source
static VALUEargf_skip(VALUE argf){ if (ARGF.init_p && ARGF.next_p == 0) { argf_close(argf); ARGF.next_p = 1; } return 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 barARGF.filename #=> "foo"ARGF.skipARGF.filename #=> "bar"
Source
static VALUEargf_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);}Returns the current offset (in bytes) of the current file inARGF.
ARGF.pos#=> 0ARGF.gets#=> "This is line one\n"ARGF.pos#=> 17
Reads each file inARGF in its entirety, returning anArray containing lines from the files. Lines are assumed to be separated bysep.
lines =ARGF.readlineslines[0]#=> "This is line one\n"
SeeIO.readlines for a full description of all options.
Returns an integer representing the numeric file descriptor for the current file. Raises anArgumentError if there isn’t a current file.
ARGF.fileno#=> 3
Source
Source
static VALUEargf_to_s(VALUE argf){ return rb_str_new2("ARGF");}Returns “ARGF”.
Source
static VALUEargf_write_io(VALUE argf){ if (!RTEST(ARGF.current_file)) { rb_raise(rb_eIOError, "not opened for writing"); } return GetWriteIO(ARGF.current_file);}ReturnsIO instance tied toARGF for writing if inplace mode is enabled.
Source
static VALUEargf_write(int argc, VALUE *argv, VALUE argf){ return rb_io_writev(argf_write_io(argf), argc, argv);}Writes each of the givenobjects if inplace mode.