Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Dir.open
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-3.3.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: 'US-ASCII').read.encoding # => #<Encoding:US-ASCII>