Method: Kernel#gets
- Defined in:
- io.c
#gets(sep = $/[, getline_args]) ⇒ String? #gets(limit[, getline_args]) ⇒ String? #gets(sep, limit[, getline_args]) ⇒ String?
Returns (and assigns to $_
) the next line from the list of files in ARGV
(or $*
), or from standard input if no files are present on the command line. Returns nil
at end of file. The optional argument specifies the record separator. The separator is included with the contents of each record. A separator of nil
reads the entire contents, and a zero-length separator reads the input one paragraph at a time, where paragraphs are divided by two consecutive newlines. If the first argument is an integer, or optional second argument is given, the returning string would not be longer than the given value in bytes. If multiple filenames are present in ARGV
, gets(nil)
will read the contents one file at a time.
ARGV << "testfile"
print while gets
produces:
This is line one
This is line two
This is line three
And so on...
The style of programming using $_
as an implicit parameter is gradually losing favor in the Ruby community.
9059 9060 9061 9062 9063 9064 9065 9066 |
# File 'io.c', line 9059
static VALUE
rb_f_gets(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_gets(argc, argv, argf);
}
return rb_funcallv(argf, idGets, argc, argv);
}
|