public Method

Kernel.gets(...)

gets(separator=$/)     string or nil

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

Source Code

/*
*  call-seq:
*     gets(separator=$/)    => string or nil
*  
*  Returns (and assigns to <code>$_</code>) the next line from the list
*  of files in +ARGV+ (or <code>$*</code>), 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 multiple filenames are present in +ARGV+,
*  +gets(nil)+ will read the contents one file at a time.
*     
*     ARGV << "testfile"
*     print while gets
*     
*  <em>produces:</em>
*     
*     This is line one
*     This is line two
*     This is line three
*     And so on...
*     
*  The style of programming using <code>$_</code> as an implicit
*  parameter is gradually losing favor in the Ruby community.
*/

static VALUE
rb_f_gets(argc, argv)
   int argc;
   VALUE *argv;
{
   VALUE line;

   if (!next_argv()) return Qnil;
   if (TYPE(current_file) != T_FILE) {
       line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
   }
   else {
       line = argf_getline(argc, argv);
   }
   rb_lastline_set(line);
   return line;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.