Path  FileTest

Generated with Razzle Dazzle Redfish

[Validate]

FileTest

Constants

SEPARATOR_PAT
(Not documented)
SEPARATOR_PAT
(Not documented)

Public Instance Methods

absolute?(path) click to toggle source

Predicate method for testing whether a path is absolute. It returns true if the pathname begins with a slash.

# File lib/path/filetest.rb, line 15
  def absolute?(path)
    !relative?(path)
  end
bin?(fname) click to toggle source

Is a file a bin/ executable?

TODO: Make more robust. Probably needs to be fixed for Windows.

# File lib/path/filetest.rb, line 36
  def bin?(fname)
    is_bin = command_paths.any? do |f|
      FileTest.exist?(File.join(f, fname))
    end
    #is_bin ? File.basename(fname) : false
    is_bin ? fname : false
  end
chop_basename(path) click to toggle source

Chop_basename(path) -> [pre-basename, basename] or nil

# File lib/path/filetest.rb, line 64
  def chop_basename(path)
    base = File.basename(path)
    if /\A#{SEPARATOR_PAT}?\z/ =~ base
      return nil
    else
      return path[0, path.rindex(base)], base
    end
  end
command_paths() click to toggle source

Return a cached list of the PATH environment variable. This is a support method used by bin?

# File lib/path/filetest.rb, line 29
  def command_paths
    @command_paths ||= ENV['PATH'].split(/[:;]/)
  end
contains?(child, parent=Dir.pwd) click to toggle source

Does the parent contain the child?

# File lib/path/filetest.rb, line 75
  def contains?(child, parent=Dir.pwd)
    parent = File.expand_path(parent)
    child = File.expand_path(child)
    child.sub(parent,'') != child
  end
relative?(path) click to toggle source

The opposite of absolute?

# File lib/path/filetest.rb, line 20
  def relative?(path)
    while r = chop_basename(path.to_s)
      path, basename = r
    end
    path == ''
  end
safe?(path) click to toggle source

Is a path considered reasonably “safe”?

TODO: Make more robust.

# File lib/path/filetest.rb, line 55
  def safe?(path)
    case path
    when *[ '/', '/*', '/**/*' ]
      return false
    end
    true
  end

Disabled; run with --debug to generate this.