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
dir.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/dir.rb, line 183
def self.open(name, encoding: nil, &block)
dir = Primitive.dir_s_open(name, encoding)
if block
begin
yield dir
ensure
Primitive.dir_s_close(dir)
end
else
dir
end
end
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>