This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Listing A Directory
CodeExpectedActual
require 'create_tree'
create_tree 'mydir' => 
  [ {'subdirectory' => [['file_in_subdirectory', 'Just a simple file.']] },
    '.hidden_file', 'ruby_script.rb', 'text_file' ]
Dir.entries('mydir')
[".", "..", ".hidden_file", "ruby_script.rb", "subdirectory",
Error! (Exception?) Here's stdout:
LoadError: no such file to load -- create_tree
	from (irb):1:in `require'
	from (irb):1
NoMethodError: undefined method `create_tree' for main:Object
	from (irb):4
Errno::ENOENT: No such file or directory - mydir
	from (irb):5:in `open'
	from (irb):5
Dir.foreach('mydir') { |x| puts x if x != "." && x != ".."}
.hidden_file
ruby_script.rb
subdirectory
text_file
Find all the "regular" files and subdirectories in mydir. This excludes
hidden files, and the special directories . and ..
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - mydir
	from (irb):6:in `open'
	from (irb):6
Dir["mydir/*"]
["mydir/ruby_script.rb", "mydir/subdirectory", "mydir/text_file"] []
Dir["mydir/*.rb"]
["mydir/ruby_script.rb"] []
Dir.open('mydir') { |d| d.grep /file/ }
[".hidden_file", "text_file"]
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - mydir
	from (irb):9:in `open'
	from (irb):9
Dir.open('mydir') { |d| d.each { |x| puts x } }
.
..
.hidden_file
ruby_script.rb
subdirectory
text_file
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - mydir
	from (irb):10:in `open'
	from (irb):10
#Get all contents other than ".", "..", and hidden files.
d = Dir.open('mydir')
d.reject { |f| f[0] == '.' }
["subdirectory", "ruby_script.rb", "text_file"]
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - mydir
	from (irb):12:in `open'
	from (irb):12
NoMethodError: undefined method `reject' for nil:NilClass
	from (irb):13
#Now the Dir object is useless until we call Dir#rewind.
d.entries.size
0
Error! (Exception?) Here's stdout:
NoMethodError: undefined method `entries' for nil:NilClass
	from (irb):15
d.rewind
d.entries.size
6
Error! (Exception?) Here's stdout:
NoMethodError: undefined method `rewind' for nil:NilClass
	from (irb):16
NoMethodError: undefined method `entries' for nil:NilClass
	from (irb):17
#Get the names of all files in the directory.
d.rewind
d.reject { |f| !File.file? File.join(d.path, f) }
[".hidden_file", "ruby_script.rb", "text_file"]
Error! (Exception?) Here's stdout:
NoMethodError: undefined method `rewind' for nil:NilClass
	from (irb):19
NoMethodError: undefined method `reject' for nil:NilClass
	from (irb):20
d.close
def File.from_dir(dir, name)
  dir = dir.path if dir.is_a? Dir
  path = File.join(dir, name)
  (File.directory?(path) ? Dir : File).open(path) { |f| yield f }
end
File.from_dir("mydir", "subdirectory") do |subdir|
 File.from_dir(subdir, "file_in_subdirectory") do |file|
    puts %{My path is #{file.path} and my contents are "#{file.read}".}
  end
end
My path is mydir/subdirectory/file_in_subdirectory and my contents are 
"Just a simple file".
Error! (Exception?) Here's stdout:
NoMethodError: undefined method `close' for nil:NilClass
	from (irb):21
Errno::ENOENT: No such file or directory - mydir/subdirectory
	from (irb):25:in `initialize'
	from (irb):25:in `from_dir'
	from (irb):27
Dir["mydir/**/*"]
["mydir/ruby_script.rb", "mydir/subdirectory", "mydir/text_file", []
Dir["mydir/**/*file*"]
["mydir/text_file", "mydir/subdirectory/file_in_subdirectory"] []
#Regex-style character classes
Dir["mydir/[rs]*"]
["mydir/ruby_script.rb", "mydir/subdirectory"] []
Dir["mydir/[^s]*"]
["mydir/ruby_script.rb", "mydir/text_file"] []
Dir["mydir/{text,ruby}*"]
["mydir/text_file", "mydir/ruby_script.rb"] []
Dir["mydir/?ub*"]
["mydir/ruby_script.rb", "mydir/subdirectory"] []
Dir["mydir/.*"]
["mydir/.", "mydir/..", "mydir/.hidden_file"] []