Class | Pathname |
In: |
lib/pathname2.rb
|
Parent: | String |
VERSION | = | '1.6.2' | The version of this library | |
MAXPATH | = | 1024 unless defined? MAXPATH | The maximum length of a path |
+ | -> | _plus_ |
pwd | -> | getwd |
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).
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 => '/'
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’, …]
Removes unnecessary ’.’ paths and ellides ’..’ paths appropriately. This method is non-destructive.
Example:
path = Pathname.new('/usr/./local/../bin') path.clean # => '/usr/bin'
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
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 ".".
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.
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'
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"
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.
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']
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.