Objects of class Dir
are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents. See also File
, page 301. The directory used in these examples contains the two regular files (config.h
and main.rb
), the parent directory (..
), and the directory itself (.
).
Enumerable
collect
, detect
, each_with_index
, entries
, find
, find_all
, grep
, include?
, map
, max
, member?
, min
, reject
, select
, sort
, to_a
** |
Matches subdirectories recursively |
* |
Matches zero or more characters |
? |
Matches any single character |
[ charSet ] |
Matches any character from the given set of characters. A range of characters is written as
charFrom- charTo. The set may be negated
with an initial uparrow (^ ). |
{ opt, opt, ... } |
Matches any one of the optional strings |
Dir["config.?"] → ["config.h"]
Dir["*.[a-z][a-z]"] → ["main.rb"]
Dir["*.[^r]*"] → ["config.h"]
Dir["*.{rb,h}"] → ["main.rb", "config.h"]
Dir["*"] → ["config.h", "main.rb"]
HOME
, or LOGDIR
. Raises a SystemCallError
(probably Errno::ENOENT
) if the target directory does not exist.
Dir.chdir("/var/spool/mail") → 0
Dir.pwd → "/var/spool/mail"
chroot(2)
for more information.
Dir.chdir("/production/secure/root")
Dir.chroot("/production/secure/root") → 0
Dir.pwd → "/"
SystemCallError
if the directory isn't empty.
SystemCallError
if the named directory
doesn't exist.
Dir.entries("testdir") → [".", "..", "config.h", "main.rb"]
nil
Dir.foreach("testdir") {|x| puts("Got " + x) }
produces:
Got .
Got ..
Got config.h
Got main.rb
Dir.chdir("/tmp") → 0
Dir.getwd → "/tmp"
Dir.[]
.
File.umask
, and are ignored on NT. Raises a SystemCallError
if the directory cannot be created. See also the discussion of permissions.nil
open
is a synonym for Dir.new
. If a block is present, it is passed aDir as a parameter. The directory is closed at the end of the block, and Dir.open
returns nil
. Dir.getwd
.
true
Dir.delete
. true
Dir.delete
.nil
IOError
.
d = Dir.new("testdir")
d.close → nil
d = Dir.new("testdir")
d.each {|x| puts ("Got " + x) }
produces:
Got .
Got ..
Got config.h
Got main.rb
nil
nil
at the end of the stream.
d = Dir.new("testdir")
d.read → "."
d.read → ".."
d.read → "config.h"
d = Dir.new("testdir")
d.read → "."
d.rewind → #<Dir:0x401b5cac>
d.read → "."
Dir#tell
.
d = Dir.new("testdir")
d.read → "."
i = d.tell
d.read → ".."
d.seek(i) → #<Dir:0x401b5cac>
d.read → ".."
Dir#seek
.
d = Dir.new("testdir")
d.tell → 0
d.read → "."
d.tell → 12
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.