Object
Replace the file extension with newext. If there is no extension 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/ext/string.rb, line 11 11: def ext(newext='') 12: return self.dup if ['.', '..'].include? self 13: if newext != '' 14: newext = (newext =~ /^\./) ? newext : ("." + newext) 15: end 16: self.chomp(File.extname(self)) << newext 17: end
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/ext/string.rb, line 127 127: def pathmap(spec=nil, &block) 128: return self if spec.nil? 129: result = '' 130: spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag| 131: case frag 132: when '%f' 133: result << File.basename(self) 134: when '%n' 135: result << File.basename(self).ext 136: when '%d' 137: result << File.dirname(self) 138: when '%x' 139: result << File.extname(self) 140: when '%X' 141: result << self.ext 142: when '%p' 143: result << self 144: when '%s' 145: result << (File::ALT_SEPARATOR || File::SEPARATOR) 146: when '%-' 147: # do nothing 148: when '%%' 149: result << "%" 150: when /%(-?\d+)d/ 151: result << pathmap_partial($1.to_i) 152: when /^%\{([^}]*)\}(\d*[dpfnxX])/ 153: patterns, operator = $1, $2 154: result << pathmap('%' + operator).pathmap_replace(patterns, &block) 155: when /^%/ 156: fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'" 157: else 158: result << frag 159: end 160: end 161: result 162: end
Explode a path into individual components. Used by pathmap.
# File lib/rake/ext/string.rb, line 22 22: def pathmap_explode 23: head, tail = File.split(self) 24: return [self] if head == self 25: return [tail] if head == '.' || tail == '/' 26: return [head, tail] if head == '/' 27: return head.pathmap_explode + [tail] 28: end
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/ext/string.rb, line 34 34: def pathmap_partial(n) 35: dirs = File.dirname(self).pathmap_explode 36: partial_dirs = 37: if n > 0 38: dirs[0...n] 39: elsif n < 0 40: dirs.reverse[0...-n].reverse 41: else 42: "." 43: end 44: File.join(partial_dirs) 45: end
Preform the pathmap replacement operations on the given path. The patterns take the form ‘pat1,rep1;pat2,rep2...’.
# File lib/rake/ext/string.rb, line 50 50: def pathmap_replace(patterns, &block) 51: result = self 52: patterns.split(';').each do |pair| 53: pattern, replacement = pair.split(',') 54: pattern = Regexp.new(pattern) 55: if replacement == '*' && block_given? 56: result = result.sub(pattern, &block) 57: elsif replacement 58: result = result.sub(pattern, replacement) 59: else 60: result = result.sub(pattern, '') 61: end 62: end 63: result 64: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.