Last active
March 20, 2016 03:02
-
-
Save sshaw/1342663 to your computer and use it in GitHub Desktop.
Ruby Pathname extensions to check for parent and subdirectory: Pathname.under? and Pathanme.above?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pathname | |
def under?(path) | |
parent, child = [ Pathname.new(path), self ].inject([]) { |a, p| a << p.expand_path.to_s.split(File::SEPARATOR) } | |
parent.drop_while { |p| p == child.shift }.none? && child.any? | |
end | |
def above?(path) | |
Pathname.new(path).under? self | |
end | |
end | |
Pathname.new("/usr/local/bin/emacs").under? "/usr" # true | |
Pathname.new("/Users/sshaw").above? "./sshaw/perl" # true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment