public Method

IO.readlines(...)

ios.readlines(sep_string=$/)     array

Reads all of the lines in ios, and returns them in anArray. Lines are separated by the optional sep_string. If sep_string is nil, the rest of the stream is returned as a single record. The stream must be opened for reading or an IOError will be raised.

f = File.new("testfile")
f.readlines[0]   #=> "This is line one\n"

Source Code

/*
*  call-seq:
*     ios.readlines(sep_string=$/)  =>   array
*  
*  Reads all of the lines in <em>ios</em>, and returns them in
*  <i>anArray</i>. Lines are separated by the optional
*  <i>sep_string</i>. If <i>sep_string</i> is <code>nil</code>, the
*  rest of the stream is returned as a single record.
*  The stream must be opened for reading or an
*  <code>IOError</code> will be raised.
*     
*     f = File.new("testfile")
*     f.readlines[0]   #=> "This is line one\n"
*/

static VALUE
rb_io_readlines(argc, argv, io)
   int argc;
   VALUE *argv;
   VALUE io;
{
   VALUE line, ary;
   VALUE rs;

   if (argc == 0) {
       rs = rb_rs;
   }
   else {
       rb_scan_args(argc, argv, "1", &rs);
       if (!NIL_P(rs)) StringValue(rs);
   }
   ary = rb_ary_new();
   while (!NIL_P(line = rb_io_getline(rs, io))) {
       rb_ary_push(ary, line);
   }
   return ary;
}
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.