The Basics

Working with directories and files in Ruby is not hard, but there are some basic terms and ideas that you must first know. Most of the functionality for working with directories and files is defined within the Dir and File classes. You can use either by creating an object of that type, that references a given directory or file, or by using the class’ methods directly. Generally speaking, if you’ll only perform a single task with a file or directory, invoking the class methods without creating an object is the norm. If you’ll need to repeatedly refer to a file or directory, creating an object makes more sense:

f = File.new('somefile')d = Dir.new('somedir')

A second consideration when deciding whether or not to create Dir and File ...

Get Ruby: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.