Today I Learned

A Hashrocket project

Ruby Pathname.to_path

In Rails code, you’ll see a path constructed and returned as a string like this:

> Rails.root.join('app', 'models', 'my_path').to_s
=> "app/models/my_path"

An alternative method, that I think reads nicely, is to call to_path:

Rails.root.join('app', 'models', 'my_path').to_path
=> "app/models/my_path"

Pathname.to_path

Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.