Class: Dir
Overview
Objects of class Dir are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents. See also File.
The directory used in these examples contains the two regular files (config.h
and main.rb
), the parent directory (..
), and the directory itself (.
).
Class Method Summary collapse
-
.chdir(*args) ⇒ Object
Changes the current working directory of the process to the given string.
-
.children(*args) ⇒ Object
Returns an array containing all of the filenames except for “.” and “..” in the given directory.
-
.chroot(string) ⇒ 0
Changes this process’s idea of the file system root.
-
.delete(dir) ⇒ Object
Deletes the named directory.
-
.each_child(*args) ⇒ Object
Calls the block once for each entry except for “.” and “..” in the named directory, passing the filename of each entry as a parameter to the block.
-
.empty?(path_name) ⇒ Boolean
Returns
true
if the named file is an empty directory,false
if it is not a directory or non-empty. -
.entries(*args) ⇒ Object
Returns an array containing all of the filenames in the given directory.
-
.exist?(file_name) ⇒ Boolean
Returns
true
if the named file is a directory,false
otherwise. -
.exists?(fname) ⇒ Boolean
:nodoc:.
-
.foreach(*args) ⇒ Object
Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
-
.getwd ⇒ Object
Returns the path to the current working directory of this process as a string.
-
.home(*args) ⇒ Object
Returns the home directory of the current user or the named user if given.
-
.mkdir(string[, integer]) ⇒ 0
Makes a new directory named by string, with permissions specified by the optional parameter anInteger.
-
.pwd ⇒ Object
Returns the path to the current working directory of this process as a string.
-
.rmdir(dir) ⇒ Object
Deletes the named directory.
-
.unlink(dir) ⇒ Object
Deletes the named directory.
Instance Method Summary collapse
-
#children ⇒ Array
Returns an array containing all of the filenames except for “.” and “..” in this directory.
-
#close ⇒ nil
Closes the directory stream.
-
#each ⇒ Object
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
-
#each_child ⇒ Object
Calls the block once for each entry except for “.” and “..” in this directory, passing the filename of each entry as a parameter to the block.
-
#fileno ⇒ Integer
Returns the file descriptor used in dir.
-
#inspect ⇒ String
Return a string describing this Dir object.
-
#path ⇒ Object
Returns the path parameter passed to dir’s constructor.
-
#pos ⇒ Object
Returns the current position in dir.
-
#pos=(integer) ⇒ Integer
Synonym for Dir#seek, but returns the position parameter.
-
#read ⇒ String?
Reads the next entry from dir and returns it as a string.
-
#rewind ⇒ Dir
Repositions dir to the first entry.
-
#seek(integer) ⇒ Dir
Seeks to a particular location in dir.
-
#tell ⇒ Object
Returns the current position in dir.
-
#to_path ⇒ Object
Returns the path parameter passed to dir’s constructor.
Methods included from Enumerable
#all?, #any?, #chain, #chunk, #chunk_while, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #filter, #filter_map, #find, #find_all, #find_index, #first, #flat_map, #grep, #grep_v, #group_by, #include?, #inject, #lazy, #map, #max, #max_by, #member?, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_a, #to_h, #uniq, #zip
Class Method Details
.chdir([ string]) ⇒ 0 .chdir([ string]) {|path| ... } ⇒ Object
Changes the current working directory of the process to the given string. When called without an argument, changes the directory to the value of the environment variable HOME
, or LOGDIR
. SystemCallError (probably Errno::ENOENT) if the target directory does not exist.
If a block is given, it is passed the name of the new current directory, and the block is executed with that as the current directory. The original working directory is restored when the block exits. The return value of chdir
is the value of the block. chdir
blocks can be nested, but in a multi-threaded program an error will be raised if a thread attempts to open a chdir
block while another thread has one open or a call to chdir
without a block occurs inside a block passed to chdir
(even in the same thread).
Dir.chdir("/var/spool/mail")
puts Dir.pwd
Dir.chdir("/tmp") do
puts Dir.pwd
Dir.chdir("/usr") do
puts Dir.pwd
end
puts Dir.pwd
end
puts Dir.pwd
produces:
/var/spool/mail
/tmp
/usr
/tmp
/var/spool/mail
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 |
# File 'dir.c', line 1049
static VALUE
dir_s_chdir(int argc, VALUE *argv, VALUE obj)
{
VALUE path = Qnil;
if (rb_check_arity(argc, 0, 1) == 1) {
path = rb_str_encode_ospath(rb_get_path(argv[0]));
}
else {
const char *dist = getenv("HOME");
if (!dist) {
dist = getenv("LOGDIR");
if (!dist) rb_raise(rb_eArgError, "HOME/LOGDIR not set");
}
path = rb_str_new2(dist);
}
if (chdir_blocking > 0) {
if (rb_thread_current() != chdir_thread)
rb_raise(rb_eRuntimeError, "conflicting chdir during another chdir block");
if (!rb_block_given_p())
rb_warn("conflicting chdir during another chdir block");
}
if (rb_block_given_p()) {
struct chdir_data args;
args.old_path = rb_str_encode_ospath(rb_dir_getwd());
args.new_path = path;
args.done = FALSE;
return rb_ensure(chdir_yield, (VALUE)&args, chdir_restore, (VALUE)&args);
}
else {
char *p = RSTRING_PTR(path);
int r = (int)(VALUE)rb_thread_call_without_gvl(nogvl_chdir, p,
RUBY_UBF_IO, 0);
if (r < 0)
rb_sys_fail_path(path);
}
return INT2FIX(0);
}
|
.children(dirname) ⇒ Array .children(dirname, encoding: enc) ⇒ Array
Returns an array containing all of the filenames except for “.” and “..” in the given directory. Will raise a SystemCallError if the named directory doesn’t exist.
The optional encoding keyword argument specifies the encoding of the directory. If not specified, the filesystem encoding is used.
Dir.children("testdir") #=> ["config.h", "main.rb"]
3142 3143 3144 3145 3146 3147 3148 3149 |
# File 'dir.c', line 3142
static VALUE
dir_s_children(int argc, VALUE *argv, VALUE io)
{
VALUE dir;
dir = dir_open_dir(argc, argv);
return rb_ensure(dir_collect_children, dir, dir_close, dir);
}
|
.chroot(string) ⇒ 0
Changes this process’s idea of the file system root. Only a privileged process may make this call. Not available on all platforms. On Unix systems, see chroot(2)
for more information.
1185 1186 1187 1188 1189 1190 1191 1192 1193 |
# File 'dir.c', line 1185
static VALUE
dir_s_chroot(VALUE dir, VALUE path)
{
path = check_dirname(path);
if (chroot(RSTRING_PTR(path)) == -1)
rb_sys_fail_path(path);
return INT2FIX(0);
}
|
.delete(string) ⇒ 0 .rmdir(string) ⇒ 0 .unlink(string) ⇒ 0
Deletes the named directory. Raises a subclass of SystemCallError if the directory isn’t empty.
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 |
# File 'dir.c', line 1265
static VALUE
dir_s_rmdir(VALUE obj, VALUE dir)
{
const char *p;
int r;
dir = check_dirname(dir);
p = RSTRING_PTR(dir);
r = (int)(VALUE)rb_thread_call_without_gvl(nogvl_rmdir, (void *)p, RUBY_UBF_IO, 0);
if (r < 0)
rb_sys_fail_path(dir);
return INT2FIX(0);
}
|
.each_child(dirname) {|filename| ... } ⇒ nil .each_child(dirname, encoding: enc) {|filename| ... } ⇒ nil .each_child(dirname) ⇒ Object .each_child(dirname, encoding: enc) ⇒ Object
Calls the block once for each entry except for “.” and “..” in the named directory, passing the filename of each entry as a parameter to the block.
If no block is given, an enumerator is returned instead.
Dir.each_child("testdir") {|x| puts "Got #{x}" }
produces:
Got config.h
Got main.rb
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 |
# File 'dir.c', line 3070
static VALUE
dir_s_each_child(int argc, VALUE *argv, VALUE io)
{
VALUE dir;
RETURN_ENUMERATOR(io, argc, argv);
dir = dir_open_dir(argc, argv);
rb_ensure(dir_each_child, dir, dir_close, dir);
return Qnil;
}
|
.empty?(path_name) ⇒ Boolean
Returns true
if the named file is an empty directory, false
if it is not a directory or non-empty.
3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 |
# File 'dir.c', line 3396
static VALUE
rb_dir_s_empty_p(VALUE obj, VALUE dirname)
{
VALUE result, orig;
const char *path;
enum {false_on_notdir = 1};
FilePathValue(dirname);
orig = rb_str_dup_frozen(dirname);
dirname = rb_str_encode_ospath(dirname);
dirname = rb_str_dup_frozen(dirname);
path = RSTRING_PTR(dirname);
#if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
{
u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
if (getattrlist(path, &al, attrbuf, sizeof(attrbuf), 0) != 0)
rb_sys_fail_path(orig);
if (*(const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
al.commonattr = 0;
al.dirattr = ATTR_DIR_ENTRYCOUNT;
if (getattrlist(path, &al, attrbuf, sizeof(attrbuf), 0) == 0) {
if (attrbuf[0] >= 2 * sizeof(u_int32_t))
return attrbuf[1] ? Qfalse : Qtrue;
if (false_on_notdir) return Qfalse;
}
rb_sys_fail_path(orig);
}
}
#endif
result = (VALUE)rb_thread_call_without_gvl(nogvl_dir_empty_p, (void *)path,
RUBY_UBF_IO, 0);
if (result == Qundef) {
rb_sys_fail_path(orig);
}
return result;
}
|
.entries(dirname) ⇒ Array .entries(dirname, encoding: enc) ⇒ Array
Returns an array containing all of the filenames in the given directory. Will raise a SystemCallError if the named directory doesn’t exist.
The optional encoding keyword argument specifies the encoding of the directory. If not specified, the filesystem encoding is used.
Dir.entries("testdir") #=> [".", "..", "config.h", "main.rb"]
3034 3035 3036 3037 3038 3039 3040 3041 |
# File 'dir.c', line 3034
static VALUE
dir_entries(int argc, VALUE *argv, VALUE io)
{
VALUE dir;
dir = dir_open_dir(argc, argv);
return rb_ensure(dir_collect, dir, dir_close, dir);
}
|
.exist?(file_name) ⇒ Boolean
Returns true
if the named file is a directory, false
otherwise.
3343 3344 3345 3346 |
# File 'dir.c', line 3343 VALUE rb_file_directory_p(void) { } |
.exists?(fname) ⇒ Boolean
:nodoc:
3350 3351 3352 3353 3354 3355 |
# File 'dir.c', line 3350
static VALUE
rb_dir_exists_p(VALUE obj, VALUE fname)
{
rb_warn_deprecated("Dir.exists?", "Dir.exist?");
return rb_file_directory_p(obj, fname);
}
|
.foreach(dirname) {|filename| ... } ⇒ nil .foreach(dirname, encoding: enc) {|filename| ... } ⇒ nil .foreach(dirname) ⇒ Object .foreach(dirname, encoding: enc) ⇒ Object
Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
If no block is given, an enumerator is returned instead.
Dir.foreach("testdir") {|x| puts "Got #{x}" }
produces:
Got .
Got ..
Got config.h
Got main.rb
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 |
# File 'dir.c', line 3000
static VALUE
dir_foreach(int argc, VALUE *argv, VALUE io)
{
VALUE dir;
RETURN_ENUMERATOR(io, argc, argv);
dir = dir_open_dir(argc, argv);
rb_ensure(dir_each, dir, dir_close, dir);
return Qnil;
}
|
.getwd ⇒ String .pwd ⇒ String
1149 1150 1151 1152 1153 |
# File 'dir.c', line 1149
static VALUE
dir_s_getwd(VALUE dir)
{
return rb_dir_getwd();
}
|
.home ⇒ Object .home("root") ⇒ Object
Returns the home directory of the current user or the named user if given.
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 |
# File 'dir.c', line 3314
static VALUE
dir_s_home(int argc, VALUE *argv, VALUE obj)
{
VALUE user;
const char *u = 0;
rb_check_arity(argc, 0, 1);
user = (argc > 0) ? argv[0] : Qnil;
if (!NIL_P(user)) {
SafeStringValue(user);
rb_must_asciicompat(user);
u = StringValueCStr(user);
if (*u) {
return rb_home_dir_of(user, rb_str_new(0, 0));
}
}
return rb_default_home_dir(rb_str_new(0, 0));
}
|
.mkdir(string[, integer]) ⇒ 0
Makes a new directory named by string, with permissions specified by the optional parameter anInteger. The permissions may be modified by the value of File::umask, and are ignored on NT. Raises a SystemCallError if the directory cannot be created. See also the discussion of permissions in the class documentation for File.
Dir.mkdir(File.join(Dir.home, ".foo"), 0700) #=> 0
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 |
# File 'dir.c', line 1225
static VALUE
dir_s_mkdir(int argc, VALUE *argv, VALUE obj)
{
struct mkdir_arg m;
VALUE path, vmode;
int r;
if (rb_scan_args(argc, argv, "11", &path, &vmode) == 2) {
m.mode = NUM2MODET(vmode);
}
else {
m.mode = 0777;
}
path = check_dirname(path);
m.path = RSTRING_PTR(path);
r = (int)(VALUE)rb_thread_call_without_gvl(nogvl_mkdir, &m, RUBY_UBF_IO, 0);
if (r < 0)
rb_sys_fail_path(path);
return INT2FIX(0);
}
|
.getwd ⇒ String .pwd ⇒ String
1149 1150 1151 1152 1153 |
# File 'dir.c', line 1149
static VALUE
dir_s_getwd(VALUE dir)
{
return rb_dir_getwd();
}
|
.delete(string) ⇒ 0 .rmdir(string) ⇒ 0 .unlink(string) ⇒ 0
Deletes the named directory. Raises a subclass of SystemCallError if the directory isn’t empty.
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 |
# File 'dir.c', line 1265
static VALUE
dir_s_rmdir(VALUE obj, VALUE dir)
{
const char *p;
int r;
dir = check_dirname(dir);
p = RSTRING_PTR(dir);
r = (int)(VALUE)rb_thread_call_without_gvl(nogvl_rmdir, (void *)p, RUBY_UBF_IO, 0);
if (r < 0)
rb_sys_fail_path(dir);
return INT2FIX(0);
}
|
.delete(string) ⇒ 0 .rmdir(string) ⇒ 0 .unlink(string) ⇒ 0
Deletes the named directory. Raises a subclass of SystemCallError if the directory isn’t empty.
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 |
# File 'dir.c', line 1265
static VALUE
dir_s_rmdir(VALUE obj, VALUE dir)
{
const char *p;
int r;
dir = check_dirname(dir);
p = RSTRING_PTR(dir);
r = (int)(VALUE)rb_thread_call_without_gvl(nogvl_rmdir, (void *)p, RUBY_UBF_IO, 0);
if (r < 0)
rb_sys_fail_path(dir);
return INT2FIX(0);
}
|
Instance Method Details
#children ⇒ Array
3119 3120 3121 3122 3123 3124 3125 |
# File 'dir.c', line 3119
static VALUE
dir_collect_children(VALUE dir)
{
VALUE ary = rb_ary_new();
dir_each_entry(dir, rb_ary_push, ary, TRUE);
return ary;
}
|
#close ⇒ nil
948 949 950 951 952 953 954 955 956 957 958 959 |
# File 'dir.c', line 948
static VALUE
dir_close(VALUE dir)
{
struct dir_data *dirp;
dirp = dir_get(dir);
if (!dirp->dir) return Qnil;
closedir(dirp->dir);
dirp->dir = NULL;
return Qnil;
}
|
#each {|filename| ... } ⇒ Dir #each ⇒ Object
798 799 800 801 802 803 |
# File 'dir.c', line 798
static VALUE
dir_each(VALUE dir)
{
RETURN_ENUMERATOR(dir, 0, 0);
return dir_each_entry(dir, dir_yield, Qnil, FALSE);
}
|
#each_child {|filename| ... } ⇒ Dir #each_child ⇒ Object
3101 3102 3103 3104 3105 3106 |
# File 'dir.c', line 3101
static VALUE
dir_each_child_m(VALUE dir)
{
RETURN_ENUMERATOR(dir, 0, 0);
return dir_each_entry(dir, dir_yield, Qnil, TRUE);
}
|
#fileno ⇒ Integer
663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'dir.c', line 663
static VALUE
dir_fileno(VALUE dir)
{
struct dir_data *dirp;
int fd;
GetDIR(dir, dirp);
fd = dirfd(dirp->dir);
if (fd == -1)
rb_sys_fail("dirfd");
return INT2NUM(fd);
}
|
#inspect ⇒ String
Return a string describing this Dir object.
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'dir.c', line 618
static VALUE
dir_inspect(VALUE dir)
{
struct dir_data *dirp;
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
if (!NIL_P(dirp->path)) {
VALUE str = rb_str_new_cstr("#<");
rb_str_append(str, rb_class_name(CLASS_OF(dir)));
rb_str_cat2(str, ":");
rb_str_append(str, dirp->path);
rb_str_cat2(str, ">");
return str;
}
return rb_funcallv(dir, idTo_s, 0, 0);
}
|
#path ⇒ String? #to_path ⇒ String?
689 690 691 692 693 694 695 696 697 |
# File 'dir.c', line 689
static VALUE
dir_path(VALUE dir)
{
struct dir_data *dirp;
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
if (NIL_P(dirp->path)) return Qnil;
return rb_str_dup(dirp->path);
}
|
#pos ⇒ Integer #tell ⇒ Integer
850 851 852 853 854 855 856 857 858 859 |
# File 'dir.c', line 850
static VALUE
dir_tell(VALUE dir)
{
struct dir_data *dirp;
long pos;
GetDIR(dir, dirp);
pos = telldir(dirp->dir);
return rb_int2inum(pos);
}
|
#pos=(integer) ⇒ Integer
907 908 909 910 911 912 |
# File 'dir.c', line 907
static VALUE
dir_set_pos(VALUE dir, VALUE pos)
{
dir_seek(dir, pos);
return pos;
}
|
#read ⇒ String?
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'dir.c', line 752
static VALUE
dir_read(VALUE dir)
{
struct dir_data *dirp;
struct dirent *dp;
GetDIR(dir, dirp);
errno = 0;
if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc);
}
else {
int e = errno;
if (e != 0) rb_syserr_fail(e, 0);
return Qnil; /* end of stream */
}
}
|
#rewind ⇒ Dir
928 929 930 931 932 933 934 935 936 |
# File 'dir.c', line 928
static VALUE
dir_rewind(VALUE dir)
{
struct dir_data *dirp;
GetDIR(dir, dirp);
rewinddir(dirp->dir);
return dir;
}
|
#seek(integer) ⇒ Dir
879 880 881 882 883 884 885 886 887 888 |
# File 'dir.c', line 879
static VALUE
dir_seek(VALUE dir, VALUE pos)
{
struct dir_data *dirp;
long p = NUM2LONG(pos);
GetDIR(dir, dirp);
seekdir(dirp->dir, p);
return dir;
}
|
#pos ⇒ Integer #tell ⇒ Integer
850 851 852 853 854 855 856 857 858 859 |
# File 'dir.c', line 850
static VALUE
dir_tell(VALUE dir)
{
struct dir_data *dirp;
long pos;
GetDIR(dir, dirp);
pos = telldir(dirp->dir);
return rb_int2inum(pos);
}
|