public Method

Dir.each

dir.each { |filename| block }   dir

Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.

d = Dir.new("testdir")
d.each  {|x| puts "Got #{x}" }

produces:

Got .
Got ..
Got config.h
Got main.rb

Source Code

/*
*  call-seq:
*     dir.each { |filename| block }  => dir
*
*  Calls the block once for each entry in this directory, passing the
*  filename of each entry as a parameter to the block.
*
*     d = Dir.new("testdir")
*     d.each  {|x| puts "Got #{x}" }
*
*  <em>produces:</em>
*
*     Got .
*     Got ..
*     Got config.h
*     Got main.rb
*/
static VALUE
dir_each(dir)
   VALUE dir;
{
   struct dir_data *dirp;
   struct dirent *dp;

   GetDIR(dir, dirp);
   rewinddir(dirp->dir);
   for (dp = readdir(dirp->dir); dp != NULL; dp = readdir(dirp->dir)) {
       rb_yield(rb_tainted_str_new(dp->d_name, NAMLEN(dp)));
       if (dirp->dir == NULL) dir_closed();
   }
   return dir;
}
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.