new(p1, p2 = v2)
public
Returns a new directory object for the
named directory.
Show source
static VALUE
dir_initialize(int argc, VALUE *argv, VALUE dir)
{
struct dir_data *dp;
rb_encoding *fsenc;
VALUE dirname, opt;
static VALUE sym_enc;
if (!sym_enc) {
sym_enc = ID2SYM(rb_intern("encoding"));
}
fsenc = rb_filesystem_encoding();
rb_scan_args(argc, argv, "11", &dirname, &opt);
if (!NIL_P(opt)) {
VALUE v, enc=Qnil;
opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
v = rb_hash_aref(opt, sym_enc);
if (!NIL_P(v)) enc = v;
if (!NIL_P(enc)) {
fsenc = rb_to_encoding(enc);
}
}
FilePathValue(dirname);
Data_Get_Struct(dir, struct dir_data, dp);
if (dp->dir) closedir(dp->dir);
dp->dir = NULL;
dp->path = Qnil;
dp->enc = fsenc;
dp->dir = opendir(RSTRING_PTR(dirname));
if (dp->dir == NULL) {
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
dp->dir = opendir(RSTRING_PTR(dirname));
}
if (dp->dir == NULL) {
rb_sys_fail(RSTRING_PTR(dirname));
}
}
dp->path = rb_str_dup_frozen(dirname);
return dir;
}