Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Pathname#children

(?::boolish with_directory) -> ::Array[::Pathname]
children(with_dirnames = true) → array_of_pathnames

Returns an array of pathnames; each represents a child of the entry represented by self, which must be an existing directory in the underlying file system.

With with_dirnames given as true (the default), each pathname contains the full entry:

Example
Pathname('lib').children.size # => 72
Pathname('lib').children.take(3)
# => [#<Pathname:lib/bundled_gems.rb>, #<Pathname:lib/bundler>, #<Pathname:lib/bundler.rb>]

With with_dirnames given as false, each pathname contains only the basename of the entry:

Example
Pathname('lib').children(false).take(3)
# => [#<Pathname:bundled_gems.rb>, #<Pathname:bundler>, #<Pathname:bundler.rb>]

Note that entries . and .. in directory are not actually children, and so are never included in the result.

See also #chardev? #chmod #blockdev? #chop_basename also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0