Method: Kernel#gets
- Defined in:
- io.c
- (String?) gets(sep = $/) - (String?) gets(limit) - (String?) gets(sep, limit)
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.
7812 7813 7814 7815 7816 7817 7818 7819 |
# File 'io.c', line 7812
static VALUE
rb_f_gets(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_gets(argc, argv, argf);
}
return rb_funcall2(argf, idGets, argc, argv);
}
|