Class: String

### Home Classes Methods

### In Files

- lib/rake/ext/string.rb

### Parent

Object

### Methods

- #ext
- #pathmap
- #pathmap_explode
- #pathmap_partial
- #pathmap_replace

### Files

- MIT-LICENSE
- README.rdoc
- TODO
- command_line_usage.rdoc
- glossary.rdoc
- proto_rake.rdoc
- rakefile.rdoc
- rational.rdoc
- rake-0.4.14.rdoc
- rake-0.4.15.rdoc
- rake-0.5.0.rdoc
- rake-0.5.3.rdoc
- rake-0.5.4.rdoc
- rake-0.6.0.rdoc
- rake-0.7.0.rdoc
- rake-0.7.1.rdoc
- rake-0.7.2.rdoc
- rake-0.7.3.rdoc
- rake-0.8.0.rdoc
- rake-0.8.4.rdoc
- rake-0.8.5.rdoc
- rake-0.8.6.rdoc
- rake-0.8.7.rdoc

### Class/Module Index

Quicksearch

- Rake
- Rake::AltSystem
- Rake::Application
- Rake::Backtrace
- Rake::Cloneable
- Rake::CompositePublisher
- Rake::DSL
- Rake::DefaultLoader
- Rake::EarlyTime
- Rake::FileCreationTask
- Rake::FileList
- Rake::FileTask
- Rake::FileUtilsExt
- Rake::FtpFile
- Rake::FtpUploader
- Rake::InvocationChain
- Rake::InvocationChain::EmptyInvocationChain
- Rake::InvocationExceptionMixin
- Rake::MakefileLoader
- Rake::MultiTask
- Rake::NameSpace
- Rake::PackageTask
- Rake::PseudoStatus
- Rake::RubyForgePublisher
- Rake::RuleRecursionOverflowError
- Rake::SshDirPublisher
- Rake::SshFilePublisher
- Rake::SshFreshDirPublisher
- Rake::Task
- Rake::TaskArgumentError
- Rake::TaskArguments
- Rake::TaskLib
- Rake::TaskManager
- Rake::TestTask
- Rake::TraceOutput
- Rake::Win32
- Rake::Win32::Win32HomeError
- CompositePublisher
- FileUtils
- Module
- Object
- SshDirPublisher
- SshFilePublisher
- SshFreshDirPublisher
- String
- Time

No matching classes.

# String

Rake extension methods for String.

### Public Instance Methods

ext(newext='') click to toggle source

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 14
    def ext(newext='')
      return self.dup if ['.', '..'].include? self
      if newext != ''
        newext = (newext =~ /^\./) ? newext : ("." + newext)
      end
      self.chomp(File.extname(self)) << newext
    end

pathmap(spec=nil, &block) click to toggle source

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 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.

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 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:

    "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 130
    def pathmap(spec=nil, &block)
      return self if spec.nil?
      result = ''
      spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag|
        case frag
        when '%f'
          result << File.basename(self)
        when '%n'
          result << File.basename(self).ext
        when '%d'
          result << File.dirname(self)
        when '%x'
          result << File.extname(self)
        when '%X'
          result << self.ext
        when '%p'
          result << self
        when '%s'
          result << (File::ALT_SEPARATOR || File::SEPARATOR)
        when '%-'
          # do nothing
        when '%%'
          result << "%"
        when /%(-?\d+)d/
          result << pathmap_partial($1.to_i)
        when /^%\{([^}]*)\}(\d*[dpfnxX])/
          patterns, operator = $1, $2
          result << pathmap('%' + operator).pathmap_replace(patterns, &block)
        when /^%/
          fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'"
        else
          result << frag
        end
      end
      result
    end

pathmap_explode() click to toggle source

Explode a path into individual components. Used by `pathmap`.

    # File lib/rake/ext/string.rb, line 25
    def pathmap_explode
      head, tail = File.split(self)
      return [self] if head == self
      return [tail] if head == '.' || tail == '/'
      return [head, tail] if head == '/'
      return head.pathmap_explode + [tail]
    end

pathmap_partial(n) click to toggle source

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 37
    def pathmap_partial(n)
      dirs = File.dirname(self).pathmap_explode
      partial_dirs =
        if n > 0
          dirs[0...n]
        elsif n < 0
          dirs.reverse[0...-n].reverse
        else
          "."
        end
      File.join(partial_dirs)
    end

pathmap_replace(patterns, &block) click to toggle source

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 53
    def pathmap_replace(patterns, &block)
      result = self
      patterns.split(';').each do |pair|
        pattern, replacement = pair.split(',')
        pattern = Regexp.new(pattern)
        if replacement == '*' && block_given?
          result = result.sub(pattern, &block)
        elsif replacement
          result = result.sub(pattern, replacement)
        else
          result = result.sub(pattern, '')
        end
      end
      result
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.