Skip to content
class method

Dir.open

(::path dirname, ?encoding: ::encoding?) -> instance
[U] (::path dirname, ?encoding: ::encoding?) { (instance) -> U } -> U
Dir.open(dirpath) → dir
Dir.open(dirpath, encoding: nil) → dir
Dir.open(dirpath) {|dir| ... } → object
Dir.open(dirpath, encoding: nil) {|dir| ... } → object

Creates a new Dir object dir for the directory at dirpath.

With no block, the method equivalent to Dir.new(dirpath, encoding):

Example
Dir.open('.') # => #<Dir:.>

With a block given, the block is called with the created dir; on block exit dir is closed and the block’s value is returned:

Example
Dir.open('.') {|dir| dir.inspect } # => "#<Dir:.>"

The value given with optional keyword argument encoding specifies the encoding for the directory entry names; if nil (the default), the file system’s encoding is used:

Example
Dir.open('.').read.encoding                       # => #<Encoding:UTF-8>
Dir.open('.', encoding: Encoding::US_ASCII).read.encoding # => #<Encoding:US-ASCII>
See also .new .pwd .mktmpdir .rmdir also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0