public Method

IO.each_line(...)

ios.each(sep_string=$/)      {|line| block }   ios
ios.each_line(sep_string=$/) {|line| block }   ios

Executes the block for every line in ios, where lines are separated by sep_string. ios must be opened for reading or an IOError will be raised.

f = File.new("testfile")
f.each {|line| puts "#{f.lineno}: #{line}" }

produces:

1: This is line one
2: This is line two
3: This is line three
4: And so on...

Source Code

/*
*  call-seq:
*     ios.each(sep_string=$/)      {|line| block }  => ios
*     ios.each_line(sep_string=$/) {|line| block }  => ios
*  
*  Executes the block for every line in <em>ios</em>, where lines are
*  separated by <i>sep_string</i>. <em>ios</em> must be opened for
*  reading or an <code>IOError</code> will be raised.
*     
*     f = File.new("testfile")
*     f.each {|line| puts "#{f.lineno}: #{line}" }
*     
*  <em>produces:</em>
*     
*     1: This is line one
*     2: This is line two
*     3: This is line three
*     4: And so on...
*/

static VALUE
rb_io_each_line(argc, argv, io)
   int argc;
   VALUE *argv;
   VALUE io;
{
   VALUE str;
   VALUE rs;

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