https://github.com/sixarm/sixarm_ruby_pathname_dirnames
SixArm.com » Ruby » Pathname#dirnames method to iterate on parent directories
https://github.com/sixarm/sixarm_ruby_pathname_dirnames
dirname gem pathname ruby
Last synced: about 1 year ago
JSON representation
SixArm.com » Ruby » Pathname#dirnames method to iterate on parent directories
- Host: GitHub
- URL: https://github.com/sixarm/sixarm_ruby_pathname_dirnames
- Owner: SixArm
- License: other
- Created: 2012-03-22T19:41:56.000Z (over 14 years ago)
- Default Branch: main
- Last Pushed: 2023-09-15T19:28:47.000Z (almost 3 years ago)
- Last Synced: 2025-02-06T00:24:38.490Z (over 1 year ago)
- Topics: dirname, gem, pathname, ruby
- Language: Ruby
- Homepage: http://sixarm.com
- Size: 248 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# SixArm.com → Ruby →
Pathname#dirnames method to iterate on parent directories
* Doc:
* Gem:
* Repo:
## Introduction
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.
Example:
p = Pathname.new('/foo/goo/hoo.txt')
p.dirnames
#=> ['/foo/goo', '/foo', '/']
For docs go to
Want to help? We're happy to get pull requests.
## Install
### Gem
To install this gem in your shell or terminal:
gem install sixarm_ruby_pathname_dirnames
### Gemfile
To add this gem to your Gemfile:
gem 'sixarm_ruby_pathname_dirnames'
### Require
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 = Pathname.new('/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.