|Updated

Ruby’s Pathname API

Share this article

Key Takeaways

  • The Pathname class in Ruby’s standard library represents a path on the file system, providing a convenient way to access functions that are usually scattered across multiple classes like File, FileTest, and Dir.
  • The Pathname API offers methods that either manipulate the path string without accessing the file system, or call out to the operating system to perform checks and operations on the file or directory itself.
  • Pathname instances are immutable, meaning there are no methods that change the Pathname instance itself. Instead, all methods like dirname, join or basename return a new Pathname instance, leaving the existing instance unchanged.
  • Pathname can be used to create powerful and intuitive APIs for tasks like browsing a music collection, by wrapping the string in a Pathname object, providing a clean API for manipulating the path.
  • The Pathname API can handle operations like joining paths, finding relative paths, checking if a path is absolute or relative, checking if a file or directory exists at a certain path, reading and writing to a file, getting the parent directory of a path, and getting the last component of a path.
Pathname docs

Ruby’s standard library is a treasure trove of useful classes, and the Pathname class is certainly one of them. Introduced in Ruby 1.9, Pathname represents a path on the file system, providing convenient access to functionality that is otherwise scattered across a handful of other classes like File, FileTest, and Dir.

Now that the life of Ruby 1.8 has officially come to an end, and 1.9 is nearing its fifth birthday, it’s high time to drop the legacy support and embrace the elegance and ease of use Pathname has to offer.

To show why Pathname was a much needed addition, let’s consider the more traditional way of manipulating path information. This is how we did things in the 1.8 days:

CONFIG_PATH = File.join(File.dirname(__FILE__), '..', 'config.yml')

Notice the use of class methods, a rare sight in a language where everything is an object. But what’s the alternative? The object-oriented thing-to-do would be to move the functionality closer to the data on which it operates. In this case, we’re simply manipulating strings, but it doesn’t really make sense to add join and dirname to String. A string is a far too generic a data type for such specific functionality.

Enter Pathname:

CONFIG_PATH = Pathname.new(__FILE__).dirname.join('..', 'config.yml')

By wrapping the string in a Pathname object, we get a clean API for manipulating the path. This code already looks a lot more like idiomatic Ruby. An important difference from Ruby’s vanilla strings is that Pathname instances are immutable. There are no methods that change the Pathname instance itself. Instead, all methods like dirname, join or basename return a new Pathname instance, leaving the existing instance unchanged.

The Pathname API

Methods on Pathname fall roughly into one of two categories: Either they simply manipulate the path string without accessing the file system, or they call out to the operating system to perform checks and operations on the file or directory itself. For the full list of eighty-something methods, you can check out the official Pathname API documentation. It’s worth noting that the docs are also available from a terminal through ri Pathname, or in irb with help 'Pathname'.

Let’s look at some methods you might find useful.

File Type and Permission Checks

pn = Pathname.new('/usr/bin/ruby')
pn.file?       # => true
pn.directory?  # => false

pn.absolute?   # => true
pn.relative?   # => false

pn.executable? # => true
pn.readable?   # => true
pn.writable?   # => false

pn.root?       # => false

Most of these correspond with methods on FileTest

File System Navigation and Querying

pn = Pathname.getwd # current working directory
pn.children   # => [ #<Pathname...>, ... ]
pn.each_child {|ch| ... }
pn = pn.parent

Pathname.glob('**/*.rb') # like Dir['**/*.rb'] but returns Pathnames

Pathname Manipulation

pn = Pathname.new('lib/mylib/awesome.rb')
pn.dirname                   # => #<Pathname:lib/mylib>
pn.basename                  # => #<Pathname:awesome.rb>
pn.extname                   # => ".rb"
pn.expand_path('/home/arne') # => #<Pathname:/home/arne/lib/mylib/awesome.rb>

Working with the Actual File

pn = Pathname.new(ENV['HOME']).join('.bashrc')
pn.size
pn.read
pn.open {|io| ... }
pn.each_line {|line| ... }
pn.rename('/tmp/foo')
pn.delete

Pathname() and to_path

There are two ways to create Pathname instances: through explicit construction as above, or through the Pathname() conversion method. While capitalized identifiers are typically reserved for classes and constants, Ruby has a number of built-in methods for converting arbitrary values to a specific type, named after the classes they convert to.

Array(1)                  # => [1]
Array(nil)                # => []
Array([1,2])              # => [1,2]
String(7)                 # => "7"
URI("http://example.com") # => #<URI::HTTP:0x1c0e URL:http://example.com>

These go hand in hand with a number of conversion hooks that objects can implement, like to_ary or to_str. Likewise, objects can implement to_path to indicate how they can be converted to a pathname. The pathname constructor is implemented in C for efficiency, but it would roughly translate to Ruby like this:

class Pathname
  def initialize(path)
    path = path.to_path if path.respond_to? :to_path
    @path = String(path)
  end
end

Say you are writing some scripts to help you manage your music collection. An AudioFile might look like this:

class AudioFile
  def initialize(path)
    @path = path
  end

  def to_path
    @path
  end

  # ...
end

Many built-in methods will wrap their file name arguments in a call to Pathname(), so you can pass in your AudioFile instance just the same.

af = AudioFile.new('...')
File.open(af) do |io|
  #...
end

When writing library code, reuse this pattern for maximum flexibility:

def read_configuration(config_file)
  parse_configuration(Pathname(config_file).read)
end

Now the caller of this code can pass in as config_file

  • a string
  • a Pathname
  • an object that responds to to_path
  • any object that can be converted with String()

Building a Music Collection Browser

To show how powerful and intuitive working with Pathname is, we’ll write a little API for browsing our music collection. The files are organized with songs grouped in directories per album, which are again grouped per artist. For example: ~/Music/Arsenal/Oyebo Soul/09_How Come.flacc.

We’re aiming for an easy to use API that looks like this:

mc = MusicCollection.new('~/Music') # => #<MusicCollection 'Music'>
mc.artists.first                    # => #<Artist 'Arsenal'>
mc.artists.first.albums.first       # => #<Album 'Oyebo Soul'>
mc.songs.count                      # => 120
mc.songs.first.path                 # => #<Pathname: ...>
mc.songs.first.play

The Song class is essentially our AudioFile class from above:

class Song
  attr_reader :path
  alias to_path path

  def initialize(path)
    @path = Pathname(path).expand_path
  end

  def name
    String(path.basename(path.extname))
  end

  def play(options = {})
    exec(options.fetch(:player, 'mplayer'), String(path))
    # OS X users can use player: '/usr/bin/afplay'
  end
end

We immediately call expand_path to normalize our input. This will expand ~ into the user’s home directory, and will resolve any relative file names (like . and ..). This way, we are sure to have an absolute file name.

Now we need to add classes for Album, Artist, and, finally, the MusicCollection itself. Since all of these are wrapper classes around a Pathname, we can pull the common plumbing into a base class.

class PathnameWrapper
  attr_reader :path
  alias to_path path

  def initialize(path)
    @path = Pathname(path).expand_path
  end

  def name
    # ...
  end

  def to_s
    # ...
  end
end

class Song < PathnameWrapper
  def play
    # ...
  end
end

Now we can add an Album and provide navigation methods back and forth. We will also add files and directories helpers to our base class, along with a to_proc class method. When passing in an object as a block with &, Ruby will use to_proc to find an implementation for the block. This way we get shorthand for typecasting all elements of a collection.

class PathnameWrapper
  # ...

  def directories
    path.children.select(&:directory?)
  end

  def files
    path.children.select(&:file?)
  end

  def self.to_proc
    ->(path) { new(path) }
  end
end

class Album < PathnameWrapper
  EXTENSIONS = %w[.flacc .m4a .mp3 .ogg .wav]

  def songs
    files.select do |file|
      EXTENSIONS.any? {|ext| ext == file.extname }
    end.map(&Song)
  end
end

class Song < PathnameWrapper
  # ...

  def album
    Album.new(path.dirname)
  end
end

To wrap things up we introduce Artist and MusicCollection.

class MusicCollection < PathnameWrapper
  def artists
    directories.map(&Artist)
  end

  def albums
    artists.flat_map(&:albums)
  end

  def songs
    albums.flat_map(&:songs)
  end
end

class Artist < PathnameWrapper
  def albums
    directories.map(&Album)
  end
end

class Album < PathnameWrapper
  # ...

  def artist
    Artist.new(path.dirname)
  end
end

class Song < PathnameWrapper
  # ...

  def artist
    album.artist
  end
end

Now we can browse back and forth between songs, albums, and artists.

This concludes the basics of our MusicCollection API, as well as our introduction to the Pathname class. Having a good grasp on Ruby’s standard library can help to make your code more elegant, correct, and concise. Here we’ve zoomed in on a single small component, highlighting its many uses. Go forward and use Pathname wisely.

Frequently Asked Questions (FAQs) about Ruby’s Pathname API

What is the primary function of Ruby’s Pathname API?

Ruby’s Pathname API is a powerful tool that allows you to manipulate file paths in a clean and object-oriented manner. It provides a set of methods to parse and manipulate file paths. It’s a more intuitive and less error-prone alternative to using strings to handle file paths. With Pathname, you can easily perform operations like joining paths, finding relative paths, and checking if a path is absolute or relative.

How do I create a new Pathname object?

Creating a new Pathname object is straightforward. You simply call the Pathname.new method and pass the path as a string. For example, Pathname.new("/home/user/documents") creates a new Pathname object for the “/home/user/documents” directory.

How can I join two paths using Pathname?

Joining two paths using Pathname is easy and intuitive. You can use the #join method, which returns a new Pathname object. For example, if you have two paths, “/home/user” and “documents”, you can join them like this: Pathname.new("/home/user").join("documents").

How can I check if a path is absolute using Pathname?

You can use the #absolute? method to check if a path is absolute. This method returns true if the path is absolute, and false otherwise. For example, Pathname.new("/home/user/documents").absolute? will return true.

How can I find the relative path between two paths using Pathname?

You can use the #relative_path_from method to find the relative path from one path to another. This method takes another Pathname object as an argument and returns a new Pathname object representing the relative path. For example, Pathname.new("/home/user/documents").relative_path_from(Pathname.new("/home/user")) will return “documents”.

How can I check if a file or directory exists at a certain path using Pathname?

You can use the #exist? method to check if a file or directory exists at the path represented by the Pathname object. This method returns true if the file or directory exists, and false otherwise. For example, Pathname.new("/home/user/documents").exist? will return true if the “/home/user/documents” directory exists.

How can I read the contents of a file using Pathname?

You can use the #read method to read the contents of a file. This method returns a string containing the contents of the file. For example, Pathname.new("/home/user/documents/my_file.txt").read will return the contents of the “my_file.txt” file.

How can I write to a file using Pathname?

You can use the #write method to write to a file. This method takes a string as an argument and writes it to the file. For example, Pathname.new("/home/user/documents/my_file.txt").write("Hello, world!") will write “Hello, world!” to the “my_file.txt” file.

How can I get the parent directory of a path using Pathname?

You can use the #parent method to get the parent directory of a path. This method returns a new Pathname object representing the parent directory. For example, Pathname.new("/home/user/documents").parent will return “/home/user”.

How can I get the last component of a path using Pathname?

You can use the #basename method to get the last component of a path. This method returns a new Pathname object representing the last component. For example, Pathname.new("/home/user/documents").basename will return “documents”.

Arne BrasseurArne Brasseur
View Author

Arne is a developer, public speaker, programming coach, author and open-source contributor. With a passion for both human and programming languages, he has spent the last decade teaching and exploring the finer points of Ruby, LISP, Haskell, Mandarin Chinese, and others. When not hopping conferences or Pacific islands, he can be found at his home base in Berlin, brewing fine teas while pondering the future of open-source and the web.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week