SEPARATOR_PATTERN | = | ( if File::ALT_SEPARATOR |
Predicate method for testing whether a path is absolute. It returns true if the pathname begins with a slash.
# File lib/facets/core/facets/filetest/relative.rb, line 9 def absolute?(path) !relative?(path) end
List File.split, but preserves the file separators.
FileTest.chop_basename('/usr/lib') #=> ['/usr/', 'lib'] FileTest.chop_basename('/') #=> nil
Returns Array of `[pre-basename, basename]` or `nil`.
This method is here simply to support the relative? and absolute? methods.
# File lib/facets/core/facets/filetest/relative.rb, line 29 def chop_basename(path) base = File.basename(path) if /\A#{SEPARATOR_PATTERN}?\z/ =~ base return nil else return path[0, path.rindex(base)], base end end
Does the parent contain the child?
# File lib/facets/core/facets/filetest/contains.rb, line 6 def contains?(child, parent=Dir.pwd) parent = File.expand_path(parent) child = File.expand_path(child) child.sub(parent,'') != child end
The opposite of absolute?
# File lib/facets/core/facets/filetest/relative.rb, line 14 def relative?(path) while r = chop_basename(path.to_s) path, basename = r end path == '' end
Is the specified directory the root directory?
CREDIT: Jeffrey Schwab
# File lib/facets/core/facets/filetest/root.rb, line 9 def root?(dir=nil) pth = File.expand_path(dir||Dir.pwd) return true if pth == '/' return true if pth =~ /^(\w:)?\/$/ false end