each()
public
Calls the block once for each entry in this
directory, passing the filename of each entry
as a parameter to the block.
If no block is given, an enumerator is returned instead.
d = Dir.new("testdir")
d.each {|x| puts "Got #{x}" }
produces:
Got .
Got ..
Got config.h
Got main.rb
Show source
static VALUE
dir_each(VALUE dir)
{
struct dir_data *dirp;
struct dirent *dp;
IF_NORMALIZE_UTF8PATH(int norm_p);
RETURN_ENUMERATOR(dir, 0, 0);
GetDIR(dir, dirp);
rewinddir(dirp->dir);
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
const char *name = dp->d_name;
size_t namlen = NAMLEN(dp);
VALUE path;
if (norm_p && has_nonascii(name, namlen) &&
!NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
path = rb_external_str_with_enc(path, dirp->enc);
}
else
path = rb_external_str_new_with_enc(name, namlen, dirp->enc);
rb_yield(path);
if (dirp->dir == NULL) dir_closed();
}
return dir;
}