Recipe 6.1. Checking to See If a File Exists
Problem
Given a filename, you want to see whether the corresponding file exists and is the right kind for your purposes.
Solution
Most of the time you'll use the File.file? predicate, which ret...
Recipe 6.2. Checking Your Access to a File
Problem
You want to see what you can do with a file: whether you have read, write, or (on Unix systems) execute permission on it.
Solution
Use the class
methods File.readable?, File.write...
Recipe 6.3. Changing the Permissions on a File
Problem
You want to control
access to a file by modifying its Unix permissions. For instance, you want to make it so that everyone on your system can read a file, but only you can write to it.
...
Recipe 6.4. Seeing When a File Was Last Used Problem
Problem
You want to see when a file was last accessed or modified.
Solution
The result of
File.stat contains a treasure trove of metadata about a file. Perhaps the most use...
Recipe 6.5. Listing a Directory
Problem
You want to list or process the files or subdirectories within a directory.
Solution
If you're starting from a directory name, you can use
Dir.entries to get an array of the items in the dir...
Recipe 6.6. Reading the Contents of a File
Problem
You want to read some or all of a file into memory.
Solution
Open the file with Kernel#open, and pass in a code block that does the actual
reading. To read the entire file into a ...
Recipe 6.7. Writing to a File
Problem
You want to write some text or Ruby data structures to a file. The file might or might not exist. If it does exist, you might want to overwrite the old contents, or just append new data to the end of t...
Recipe 6.8. Writing to a Temporary File
Problem
You want to write data to a secure
temporary file with a unique name.
Solution
Create a
Tempfile object. It has all the methods of a File object, and it will be in a location...
Recipe 6.9. Picking a Random Line from a File
Problem
You want to choose a random line from a file, without loading the entire file into memory.
Solution
Iterate over the file, giving each line a chance to be the randomly selected one:
...
Recipe 6.10. Comparing Two Files
Problem
You want to see if two files contain the same data. If they differ, you might want to represent the differences between them as a string: a patch from one to the other.
Solution
If two files d...
Recipe 6.11. Performing Random Access on "Read-Once" Input Streams
Problem
You have an IO object, probably a socket, that doesn't support random-access methods like seek, pos=, and rewind. You want to treat this object like a file on disk,...
Recipe 6.12. Walking a Directory Tree
Problem
You want to
recursively process every subdirectory and file within a certain directory.
Solution
Suppose that the directory tree you want to walk looks like this (see this chapter's introdu...
Recipe 6.13. Locking a File
Problem
You want to prevent other threads or processes from modifying a file that you're working on.
Solution
Open the file, then lock it with
File#flock. There are two kinds of lock; pass in the File c...
Recipe 6.14. Backing Up to Versioned Filenames
Problem
You want to copy a file to a numbered backup before overwriting the original file. More generally: rather than overwriting an existing file, you want to use a new file whose name ...
Recipe 6.15. Pretending a String Is a File
Problem
You want to call code that expects to read from an open file object, but your source is a string in memory. Alternatively, you want to call code that writes its output to a file, but have it ac...
Recipe 6.16. Redirecting Standard Input or Output
Problem
You don't want the standard input, output, or error of your process to go to the
default IO objects set up by the Ruby interpreter. You want them to go to other filetype objec...
Recipe 6.17. Processing a Binary File
Problem
You want to read binary data from a file, or write it to one.
Solution
Since Ruby strings make no distinction between binary and text data, processing a binary file needn't be any differe...
Recipe 6.18. Deleting a File
Problem
You want to delete a single file, or a whole directory tree.
Solution
Removing a file is simple, with File.delete:
import 'fileutils'
FileUtils.touch "doomed_file"
File.exists? "doomed_fil...
Recipe 6.19. Truncating a File
Problem
You want to truncate a file to a certain length, probably zero bytes.
Solution
Usually, you want to destroy the old contents of a file and start over. Opening a file for write access will automa...
Recipe 6.20. Finding the Files You Want
Problem
You want to locate all the files in a directory hierarchy that match some criteria. For instance, you might want to find all the empty files, all the MP3 files, or all the files named "README."
...
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.