Skip to content

Instantly share code, notes, and snippets.

@sshaw
Last active March 20, 2016 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshaw/1342663 to your computer and use it in GitHub Desktop.
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?
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