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#opendir

() -> ::Dir
[U] () { (::Dir) -> U } -> U
opendir {|dir| ... } → object
opendir → dir

Creates a Dir object dir for the directory at the path represented by self; opens dir.

With a block given, calls the block with dir; on block exit, closes dir and returns the block’s return value:

Example
pn = Pathname('.')
pn.opendir {|dir| dir.entries.take(3) }
# => ["README.md", "html", ".git"]

With no block given, returns the open directory dir:

Example
dir = pn.opendir    # => #<Dir:.>
dir.entries.take(3) # => ["README.md", "html", ".git"]
dir.close
See also #open #owned? #mtime #parent also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0