12.3. Querying IO and File objects

IO and File objects can be queried on numerous criteria. The IO class includes some query methods; the File class adds more.

One class and one module closely related to File also get into the act: File::Stat and FileTest. File::Stat returns objects whose attributes correspond to the fields of the stat structure defined by the C library call stat(2). Some of these fields are system-specific and not meaningful on all platforms. The FileTest module offers numerous methods for getting status information about files.

The File class also has some query methods. In some cases, you can get the same information about a file several ways:

>> File.size("code/ticket2.rb") => 219 >> FileTest.size("code/ticket2.rb") => 219 ...

Get The Well-Grounded Rubyist, Second Edition 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.