Module: NanDoc::RegexpEnhance::MatchData

Defined in:
lib/nandoc/support/regexp-enhance.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to(md, re) ⇒ Object



53
54
55
56
57
# File 'lib/nandoc/support/regexp-enhance.rb', line 53

def to(md, re)
  md.extend self
  md.match_data_enhanced_init(re)
  nil
end

Instance Method Details

#match_data_enhanced_init(re) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nandoc/support/regexp-enhance.rb', line 59

def match_data_enhanced_init re
  @names = re.names
  class << self # @todo see if this doesn't break by moving defs out
    alias_method :fetch_orig, :[]
    attr_reader :names
    def [](mixed)
      return fetch_orig(mixed) unless mixed.kind_of?(Symbol)
      fail("no such named capture: #{mixed.inspect}") unless
        @names.include?(mixed)
      offset = @names.index(mixed) + 1
      fetch_orig offset
    end
  end
end

#to_hashHash

Returns of the named captures in the MatchData.

Returns:

  • (Hash)

    of the named captures in the MatchData



77
78
79
# File 'lib/nandoc/support/regexp-enhance.rb', line 77

def to_hash
  Hash[ names.map{ |n| [n, self[n] ] } ]
end