File

The File class in Ruby does two things. First, it’s a subclass of IO, meaning it handles reading data from and writing data to files. Second, it adds a number of methods for manipulating files as objects, similar to the Dir class. Since the reading and writing behavior is mostly managed by methods defined in IO, we’ll discuss it when we look at IO in IO. Here, we’ll look at file-specific manipulations.

Most File manipulation methods are class methods, while most of the read/write methods are instance methods.

Opening a File

You create a File instance with File.new(filename, mode = "r", permissions = 0666, *options) or File.open(filename, mode = "r", permissions = 0666, *options) [{block}]. (Note that this doesn’t necessarily create the ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.