January 2024
Intermediate to advanced
718 pages
20h 15m
English
You can create a new file object using File.new:
| | file = File.new("testfile", "r") |
| | # ... process the file |
| | file.close |
The first parameter to the method is the filename. The second is called the mode string, which lets you declare whether you’re opening the file for reading, writing, or both. Here we opened testfile for reading with an "r". We could also have used "w" for write or "r+" for read-write. The full list of allowed modes appears in Table 28, Mode values.
You can also optionally specify file permissions when creating a file. After opening the file, we can write and/or read data as needed and as specified by the mode string. When we’re done, as responsible software citizens, we close the file, ...
Read now
Unlock full access