User defined methods to be added to String.

Methods
Public Instance methods
ext(newext='')

Replace the file extension with newext. If there is no extenson on the string, append the new extension to the end. If the new extension is not given, or is the empty string, remove any existing extension.

ext is a user added method for the String class.

    # File lib/rake.rb, line 73
73:     def ext(newext='')
74:       return self.dup if ['.', '..'].include? self
75:       if newext != ''
76:         newext = (newext =~ /^\./) ? newext : ("." + newext)
77:       end
78:       dup.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || self + newext
79:     end
pathmap(spec=nil, &block)

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

  • %p — The complete path.
  • %f — The base file name of the path, with its file extension, but without any directories.
  • %n — The file name of the path without its file extension.
  • %d — The directory list of the path.
  • %x — The file extension of the path. An empty string if there is no extension.
  • %X — Everything but the file extension.
  • %s — The alternate file separater 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.

Examples:

  '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 substititions on a particular part of the path. The pattern and replacement are speparated 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"). Muliple 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:

   "src/org/onestepback/proj/A.java".pathmap("%{^src,bin}X.class")

returns:

   "bin/org/onestepback/proj/A.class"

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|
     ext.downcase
  }

Returns:

 "/path/to/file.txt"
     # File lib/rake.rb, line 198
198:     def pathmap(spec=nil, &block)
199:       return self if spec.nil?
200:       result = ''
201:       spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag|
202:         case frag
203:         when '%f'
204:           result << File.basename(self)
205:         when '%n'
206:           result << File.basename(self).ext
207:         when '%d'
208:           result << File.dirname(self)
209:         when '%x'
210:           result << $1 if self =~ /[^\/](\.[^.]+)$/
211:         when '%X'
212:           if self =~ /^(.+[^\/])(\.[^.]+)$/
213:             result << $1
214:           else
215:             result << self
216:           end
217:         when '%p'
218:           result << self
219:         when '%s'
220:           result << (File::ALT_SEPARATOR || File::SEPARATOR)
221:         when '%%'
222:           result << "%"
223:         when /%(-?\d+)d/
224:           result << pathmap_partial($1.to_i)
225:         when /^%\{([^}]*)\}(\d*[dpfnxX])/
226:           patterns, operator = $1, $2
227:           result << pathmap('%' + operator).pathmap_replace(patterns, &block)
228:         when /^%/
229:           fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'"
230:         else
231:           result << frag
232:         end
233:       end
234:       result
235:     end
pathmap_explode()

Explode a path into individual components. Used by pathmap.

    # File lib/rake.rb, line 85
85:     def pathmap_explode
86:       head, tail = File.split(self)
87:       return [self] if head == self
88:       return [tail] if head == '.' || tail == '/'
89:       return [head, tail] if head == '/'
90:       return head.pathmap_explode + [tail]
91:     end
pathmap_partial(n)

Extract a partial path from the path. Include n directories from the front end (left hand side) if n is positive. Include |n| directories from the back end (right hand side) if n is negative.

     # File lib/rake.rb, line 98
 98:     def pathmap_partial(n)
 99:       target = File.dirname(self)
100:       dirs = target.pathmap_explode
101:       if n > 0
102:         File.join(dirs[0...n])
103:       elsif n < 0
104:         partial = dirs[n..-1]
105:         if partial.nil? || partial.empty?
106:           target
107:         else
108:           File.join(partial)
109:         end
110:       else
111:         "."
112:       end
113:     end
pathmap_replace(patterns, &block)

Preform the pathmap replacement operations on the given path. The patterns take the form ‘pat1,rep1;pat2,rep2…’.

     # File lib/rake.rb, line 118
118:     def pathmap_replace(patterns, &block)
119:       result = self
120:       patterns.split(';').each do |pair|
121:         pattern, replacement = pair.split(',')
122:         pattern = Regexp.new(pattern)
123:         if replacement == '*' && block_given?
124:           result = result.sub(pattern, &block)
125:         elsif replacement
126:           result = result.sub(pattern, replacement)
127:         else
128:           result = result.sub(pattern, '')
129:         end
130:       end
131:       result
132:     end