stdlib/dir/reading_a_directory.rb

tests/_examples/stdlib/dir/reading_a_directory.rb in the metorex repository.

# The questions Dir answers about a directory without opening it.
root = File.join(Dir.tmpdir, "metorex_dir_example")
if Dir.exist?(root)
  Dir.each_child(root) { |name| File.delete(File.join(root, name)) }
  Dir.delete(root)
end
Dir.mkdir(root)
p(Dir.empty?(root))

File.write(File.join(root, "held.txt"), "content")
p(Dir.empty?(root))
p(Dir.empty?(File.join(root, "held.txt")))

p(Dir.entries(root).sort)
p(Dir.children(root))

names = []
Dir.foreach(root) { |name| names << name }
p(names.sort)

children = []
Dir.each_child(root) { |name| children << name }
p(children)

p(Dir.foreach(root).class)
p(Dir.each_child(root).size)

# A directory that still holds something cannot be removed, and one that was
# never there cannot either.
begin
  Dir.rmdir(root)
rescue Errno::ENOTEMPTY => problem
  p(problem.class)
end
begin
  Dir.unlink(File.join(root, "nowhere"))
rescue Errno::ENOENT => problem
  p(problem.class)
end

File.delete(File.join(root, "held.txt"))
p(Dir.delete(root))
p(Dir.exist?(root))
p(Dir.home == ENV["HOME"])

Output:

true
false
false
[".", "..", "held.txt"]
["held.txt"]
[".", "..", "held.txt"]
["held.txt"]
Enumerator
nil
Errno::ENOTEMPTY
Errno::ENOENT
0
false
true