Skip to Content
The Ruby Programming Language
book

The Ruby Programming Language

by David Flanagan, Yukihiro Matsumoto
January 2008
Beginner to intermediate
446 pages
14h 45m
English
O'Reilly Media, Inc.
Content preview from The Ruby Programming Language

Files and Directories

The File class defines quite a few class methods for working with files as entries in a filesystem: methods for testing the size or existence of a named file, for example, and methods for separating a filename from the directory name that precedes it. These are class methods and they do not operate on File objects; instead, filenames are specified as strings. Similarly, the Dir class defines class methods for working with and reading filenames from filesystem directories. The subsections that follow demonstrate how to:

  • Work with and manipulate filenames and directory names

  • List directories

  • Test files to determine their type, size, modification time, and other attributes

  • Delete, rename, and perform similar operations on files and directories

Note that the methods described here query and manipulate files, but do not read or write file content. Reading and writing files is covered in Input/Output.

File and Directory Names

The class methods of the File and Dir classes operate on files and directories specified by name. Ruby uses Unix-style filenames with / as the directory separator character. You can use the forward slash character in your filenames, even when using ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Metaprogramming Ruby 2

Metaprogramming Ruby 2

Paolo Perrotta

Publisher Resources

ISBN: 9780596516178Supplemental ContentErrata Page