End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Pathname#entries
entries
ext/pathname/pathname.c
View on GitHub ↗
static VALUE
path_entries(VALUE self)
{
VALUE klass, str, ary;
long i;
klass = rb_obj_class(self);
str = get_strpath(self);
ary = rb_funcall(rb_cDir, id_entries, 1, str);
ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
for (i = 0; i < RARRAY_LEN(ary); i++) {
VALUE elt = RARRAY_AREF(ary, i);
elt = rb_class_new_instance(1, &elt, klass);
rb_ary_store(ary, i, elt);
}
return ary;
}
Return the entries (files and subdirectories) in the directory, each as a Pathname object.
The results contains just the names in the directory, without any trailing slashes or recursive look-up.
Example
pp Pathname.new('/usr/local').entries #=> [#<Pathname:share>, # #<Pathname:lib>, # #<Pathname:..>, # #<Pathname:include>, # #<Pathname:etc>, # #<Pathname:bin>, # #<Pathname:man>, # #<Pathname:games>, # #<Pathname:.>, # #<Pathname:sbin>, # #<Pathname:src>]
The result may contain the current directory #<Pathname:.> and the parent directory #<Pathname:..>.
If you don’t want . and .. and want directories, consider Pathname#children.