Ruby latest stable (v2_5_5) - 0 notes - Class: String

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2_2_9) is shown here.

pathmap(spec=nil, &block) public

Map the path according to the given specification. The specification controls the details of the mapping. The following special patterns are recognized:


The complete path.


The base file name of the path, with its file extension, but without any directories.


The file name of the path without its file extension.


The directory list of the path.


The file extension of the path. An empty string if there is no extension.


Everything but the file extension.


The alternate file separator if defined, otherwise use # the standard file separator.


A percent sign.

The %d specifier can also have a numeric prefix (e.g. ‘%2d’). If the number is positive, only return (up to) n directories in the path, starting from the left hand side. If n is negative, return (up to) n directories from the right hand side of the path.


'a/b/c/d/file.txt'.pathmap("%2d")   => 'a/b'
'a/b/c/d/file.txt'.pathmap("%-2d")  => 'c/d'

Also the %d, %p, %f, %n, %x, and %X operators can take a pattern/replacement argument to perform simple string substitutions on a particular part of the path. The pattern and replacement are separated by a comma and are enclosed by curly braces. The replacement spec comes after the % character but before the operator letter. (e.g. “%{old,new}d”). Multiple replacement specs should be separated by semi-colons (e.g. “%{old,new;src,bin}d”).

Regular expressions may be used for the pattern, and back refs may be used in the replacement text. Curly braces, commas and semi-colons are excluded from both the pattern and replacement text (let’s keep parsing reasonable).

For example:




If the replacement text is ‘*’, then a block may be provided to perform some arbitrary calculation for the replacement.

For example:

"/path/to/file.TXT".pathmap("%X%{.*,*}x") { |ext|



This String extension comes from Rake

Show source
Register or log in to add new notes.