Class Pathname
In: lib/pathname2.rb
Parent: String

Methods

+   /   <=>   []   absolute?   ascend   basename   cd   chdir   children   chmod   chown   clean   clean!   cleanpath   cleanpath!   compare_file   copy_entry   copy_file   cp   cp_r   descend   dirname   drive_number   each   entries   expand_path   find   fnmatch   fnmatch?   foreach   glob   install   join   lchmod   lchown   link   ln   ln_s   ln_sf   long_path   mkdir   mkdir_p   mkpath   mv   new   open   opendir   parent   pstrip   pstrip!   pwd   read   readlines   realpath   relative?   relative_path_from   remove   remove_dir   remove_file   rename   rm   rm_f   rm_r   rm_rf   rmtree   root   root?   short_path   symlink   sysopen   to_a   touch   truncate   unc?   undecorate   undecorate!   uptodate?   utime  

Included Modules

Windows::Path Windows::File Windows::Error Windows::Limits

Classes and Modules

Class Pathname::Error

Constants

VERSION = '1.6.2'   The version of this library
MAXPATH = 1024 unless defined? MAXPATH   The maximum length of a path

External Aliases

+ -> _plus_
pwd -> getwd

Public Class methods

Creates and returns a new Pathname object.

On platforms that define File::ALT_SEPARATOR, all forward slashes are replaced with the value of File::ALT_SEPARATOR. On MS Windows, for example, all forward slashes are replaced with backslashes.

File URL‘s will be converted to Pathname objects, e.g. the file URL "file:///C:/Documents%20and%20Settings" will become ‘C:\Documents and Settings’.

Examples:

  Pathname.new("/foo/bar/baz"
  Pathname.new("foo")
  Pathname.new("file:///foo/bar/baz")
  Pathname.new("C:\\Documents and Settings\\snoopy")

Returns the expanded path of the current working directory.

Synonym for Pathname.new(Dir.pwd).

Public Instance methods

Adds two Pathname objects together, or a Pathname and a String. It also automatically cleans the Pathname.

Adding a root path to an existing path merely replaces the current path. Adding ’.’ to an existing path does nothing.

Example:

   path1 = '/foo/bar'
   path2 = '../baz'
   path1 + path2 # '/foo/baz'
/(string)

Alias for #+

Compares two Pathname objects. Note that Pathnames may only be compared against other Pathnames, not strings. Otherwise nil is returned.

Example:

   path1 = Pathname.new('/usr/local')
   path2 = Pathname.new('/usr/local')
   path3 = Pathname.new('/usr/local/bin')

   path1 <=> path2 # => 0
   path1 <=> path3 # => -1

Returns the path component at index, up to length components, joined by the path separator. If the index is a Range, then that is used instead and the length is ignored.

Keep in mind that on MS Windows the drive letter is the first element.

Examples:

   path = Pathname.new('/home/john/source/ruby')
   path[0]    # => 'home'
   path[1]    # => 'john'
   path[0, 3] # => '/home/john/source'
   path[0..1] # => '/home/john'

   path = Pathname.new('C:/Documents and Settings/John/Source/Ruby')
   path[0]    # => 'C:\'
   path[1]    # => 'Documents and Settings'
   path[0, 3] # => 'C:\Documents and Settings\John'
   path[0..1] # => 'C:\Documents and Settings'

Returns whether or not the path is an absolute path.

Example:

   Pathname.new('/usr/bin').absolute? # => true
   Pathname.new('usr').absolute?      # => false

Yields the path, minus one component on each iteration, as a new Pathname object, ending with the root path.

Example:

   path = Pathname.new('/usr/local/bin')

   path.ascend{ |name|
      puts name
   }

   First iteration  => '/usr/local/bin'
   Second iteration => '/usr/local'
   Third iteration  => '/usr'
   Fourth iteration => '/'

File.basename

FileUtils.cd

Dir.chdir

Returns the children of the directory, files and subdirectories, as an array of Pathname objects. If you set with_directory to false, then the returned pathnames will contain the filename only.

Note that the result never contain the entries ’.’ and ’..’ in the the directory because they are not children. Also note that this method is not recursive.

Example:

path = Pathname.new(’/usr/bin’) path.children # => [’/usr/bin/ruby’, ’/usr/bin/perl’, …] path.children(false) # => [‘ruby’, ‘perl’, …]

File.chmod

Removes unnecessary ’.’ paths and ellides ’..’ paths appropriately. This method is non-destructive.

Example:

   path = Pathname.new('/usr/./local/../bin')
   path.clean # => '/usr/bin'

Identical to Pathname#clean, except that it modifies the receiver in place.

cleanpath()

Alias for clean

cleanpath!()

Alias for clean!

FileUtils.compare_file

FileUtils.copy_entry

FileUtils.copy_file

FileUtils.cp

FileUtils.cp_r

Yields each component of the path, concatenating the next component on each iteration as a new Pathname object, starting with the root path.

Example:

   path = Pathname.new('/usr/local/bin')

   path.descend{ |name|
      puts name
   }

   First iteration  => '/'
   Second iteration => '/usr'
   Third iteration  => '/usr/local'
   Fourth iteration => '/usr/local/bin'

Similar to File.dirname, but this method allows you to specify the number of levels up you wish to refer to.

The default level is 1, i.e. it works the same as File.dirname. A level of 0 will return the original path. A level equal to or greater than the number of path elements will return the root path.

A number less than 0 will raise an ArgumentError.

Example:

   path = Pathname.new('/usr/local/bin/ruby')

   puts path.dirname    # => /usr/local/bin
   puts path.dirname(2) # => /usr/local
   puts path.dirname(3) # => /usr
   puts path.dirname(9) # => /

MS Windows only

Returns the drive number that corresponds to the root, or nil if not applicable.

Example:

   Pathname.new("C:\\foo").drive_number # => 2

Yields each component of the path name to a block.

Example:

   Pathname.new('/usr/local/bin').each{ |element|
      puts "Element: #{element}"
   }

   Yields 'usr', 'local', and 'bin', in turn

Dir.entries

File.expand_path

Pathname#find is an iterator to traverse a directory tree in a depth first manner. It yields a Pathname for each file under the directory passed to Pathname.new.

Since it is implemented by the Find module, Find.prune can be used to control the traverse.

If self is ".", yielded pathnames begin with a filename in the current current directory, not ".".

Dir.glob

:no-doc: This differs from Tanaka‘s implementation in that it does a temporary chdir to the path in question, then performs the glob.

FileUtils.install

File.join

File.lchmod

File.link

FileUtils.ln

FileUtils.ln_s

FileUtils.ln_sf

Windows only

Returns the long path for a long path name.

Example:

   path = Pathname.new('C:\Progra~1\Java')
   path.long_path # => C:\Program Files\Java.

Dir.mkdir

FileUtils.mkdir_p

mkpath(*args)

Alias for mkdir_p

FileUtils.mv

Dir.opendir

Returns the parent directory of the given path.

Example:

   Pathname.new('/usr/local/bin').parent # => '/usr/local'

Removes trailing slash, if present. Non-destructive.

Example:

   path = Pathname.new('/usr/local/')
   path.pstrip # => '/usr/local'

Performs the substitution of Pathname#pstrip in place.

IO.readlines

Returns a real (absolute) pathname of self in the actual filesystem.

Unlike most Pathname methods, this one assumes that the path actually exists on your filesystem. If it doesn‘t, an error is raised. If a circular symlink is encountered a system error will be raised.

Example:

   Dir.pwd                      # => /usr/local
   File.exists?('foo')          # => true
   Pathname.new('foo').realpath # => /usr/local/foo

Returns whether or not the path is a relative path.

Example:

   Pathname.new('/usr/bin').relative? # => true
   Pathname.new('usr').relative?      # => false

Returns a relative path from the argument to the receiver. If self is absolute, the argument must be absolute too. If self is relative, the argument must be relative too. For relative paths, this method uses an imaginary, common parent path.

This method does not access the filesystem. It assumes no symlinks. You should only compare directories against directories, or files against files, or you may get unexpected results.

Raises an ArgumentError if it cannot find a relative path.

Examples:

   path = Pathname.new('/usr/local/bin')
   path.relative_path_from('/usr/bin') # => "../local/bin"

   path = Pathname.new("C:\\WINNT\\Fonts")
   path.relative_path_from("C:\\Program Files") # => "..\\WINNT\\Fonts"
remove(*args)

Alias for rm

FileUtils.remove_dir

FileUtils.remove_file

File.rename

FileUtils.rm

FileUtils.rm_f

FileUtils.rm_r

FileUtils.rm_rf

FileUtils.rmtree

Returns the root directory of the path, or ’.’ if there is no root directory.

On Unix, this means the ’/’ character. On Windows, this can refer to the drive letter, or the server and share path if the path is a UNC path.

Examples:

   Pathname.new('/usr/local').root       # => '/'
   Pathname.new('lib').root              # => '.'

   On MS Windows:

   Pathname.new('C:\WINNT').root         # => 'C:'
   Pathname.new('\\some\share\foo').root # => '\\some\share'

Returns whether or not the path consists only of a root directory.

Examples:

  Pathname.new('/').root?    # => true
  Pathname.new('/foo').root? # => false

Windows only

Returns the short path for a long path name.

Example:

   path = Pathname.new('C:\Program Files\Java')
   path.short_path # => C:\Progra~1\Java.

File.symlink

IO.sysopen

Splits a pathname into strings based on the path separator.

Examples:

   Pathname.new('/usr/local/bin').to_a # => ['usr', 'local', 'bin']
   Pathname.new('C:\WINNT\Fonts').to_a # => ['C:', 'WINNT', 'Fonts']

FileUtils.touch

File.truncate

MS Windows only

Determines if the string is a valid Universal Naming Convention (UNC) for a server and share path.

Examples:

   Pathname.new("\\\\foo\\bar").unc?     # => true
   Pathname.new('C:\Program Files').unc? # => false

Windows only

Removes the decoration from a path string. Non-destructive.

Example:

path = Pathname.new(‘C:\Path\File[5].txt’) path.undecorate # => C:\Path\File.txt.

Windows only

Performs the substitution of Pathname#undecorate in place.

FileUtils.uptodate?

[Validate]