Class: Rack::Mount::RegexpWithNamedGroups

### Home Classes Methods

### In Files

- lib/rack/mount/regexp_with_named_groups.rb

### Parent

Regexp

### Methods

- ::new
- #eql?
- #named_captures
- #names

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Rack
- Rack::Mount
- Rack::Mount::Analysis
- Rack::Mount::Analysis::Splitting
- Rack::Mount::Analysis::Splitting::Key
- Rack::Mount::GeneratableRegexp
- Rack::Mount::GeneratableRegexp::InstanceMethods
- Rack::Mount::RegexpWithNamedGroups
- Rack::Mount::Route
- Rack::Mount::RouteSet
- Rack::Mount::RoutingError
- Rack::Mount::Strexp
- Rack::Mount::StrexpParser
- Rack::Mount::StrexpParser::ScanError
- Rack::Mount::Utils
- Regin
- Regin::Alternation
- Regin::Anchor
- Regin::Atom
- Regin::Character
- Regin::CharacterClass
- Regin::Collection
- Regin::Expression
- Regin::Group
- Regin::Options
- Regin::Parser
- Regin::Parser::ScanError
- Multimap
- Multiset
- NestedMultimap

No matching classes.

# Rack::Mount::RegexpWithNamedGroups

A wrapper that adds shim named capture support to older versions of Ruby.

Because the named capture syntax causes a parse error, an alternate syntax is used to indicate named captures.

Ruby 1.9+ named capture syntax:

    /(?<foo>[a-z]+)/

Ruby 1.8 shim syntax:

    /(?:<foo>[a-z]+)/

### Public Class Methods

new(regexp) click to toggle source

Wraps Regexp with named capture support.

    # File lib/rack/mount/regexp_with_named_groups.rb, line 30
    def initialize(regexp)
      regexp = Regexp.compile(regexp) unless regexp.is_a?(Regexp)
      source, options = regexp.source, regexp.options
      @names, scanner = [], StringScanner.new(source)

      while scanner.skip_until(/\(/)
        if scanner.scan(/\?:<([^>]+)>/)
          @names << scanner[1]
        elsif scanner.scan(/\?(i?m?x?\-?i?m?x?)?:/)
          # ignore noncapture
        else
          @names << nil
        end
      end
      source.gsub!(/\?:<([^>]+)>/, '')

      @names = [] unless @names.any?
      @names.freeze

      super(source, options)
    end

### Public Instance Methods

eql?(other) click to toggle source

    # File lib/rack/mount/regexp_with_named_groups.rb, line 64
    def eql?(other)
      super && @names.eql?(other.names)
    end

named_captures() click to toggle source

    # File lib/rack/mount/regexp_with_named_groups.rb, line 56
    def named_captures
      named_captures = {}
      names.each_with_index { |n, i|
        named_captures[n] = [i+1] if n
      }
      named_captures
    end

names() click to toggle source

    # File lib/rack/mount/regexp_with_named_groups.rb, line 52
    def names
      @names.dup
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.