Created: 2012-03-22 19:41
Updated: 2017-08-10 16:35
License: other → Ruby →
Pathname#dirnames method to iterate on parent directories


This gem has one method: Pathname#dirnames.

It will return an enumerable of pathnames created by calling #dirname repeatedly.

This can be useful for traversing directories upwards to parent, grandparent, etc.


 p ='/foo/goo/hoo.txt')
 #=> ['/foo/goo', '/foo', '/']

For docs go to

Want to help? We're happy to get pull requests.



Run this command in your shell or terminal:

gem install sixarm_ruby_pathname_dirnames

Or add this to your Gemfile:

gem 'sixarm_ruby_pathname_dirnames'


To require the gem in your code:

require 'sixarm_ruby_pathname_dirnames'

Example to find a file

To find the first occurance of a file named "my.txt" in a path or its parents:

 basename = "my.txt"
 p ='/foo/goo/hoo/*')
 puts p.dirnames.find{|dirname| (dirname + basename).exist?}

Note that the "" at the end of the pathname is to give dirname something to chop off; the "" is being used as chaff, not as a file matcher nor string matches.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more