class MatchData

Public Instance Methods

each() { |v| ... } click to toggle source

Iterates over each capture group in the MatchData object, including +$&+ (the entire matched string), yielding the captured string.

# File lib/mug/matchdata/each.rb, line 7
def each &_b
  return enum_for(:each) unless block_given?
  to_a.each{|v| yield v }
end
each_capture() { |i| ... } click to toggle source

Iterates over each capture group in the MatchData object, yielding the capture position and captured string.

The capture positions are either all Strings or all Integers, depending on whether the original Regexp had named capture groups or not.

# File lib/mug/matchdata/each.rb, line 18
def each_capture &_b
  return enum_for(:each_capture) unless block_given?
  if names.empty?
    captures.each_with_index{|v,i| yield i+1, v }
  else
    names.each{|n| yield n, self[n] }
  end
end
each_named_capture() { |n, self| ... } click to toggle source

Iterates over each named capture group in the MatchData object, yielding the capture name and string.

# File lib/mug/matchdata/each.rb, line 29
def each_named_capture &_b
  return enum_for(:each_named_capture) unless block_given?
  names.each{|n| yield n, self[n] }
end
each_positional_capture(include_names: false) { |i| ... } click to toggle source

Iterates over each positional capture group in the MatchData object, yielding the capture position and string.

If include_names is given and true, treats named captures as positional captures.

WARNING: if mixing named and positional captures, no positional captures will be available using this method!

# File lib/mug/matchdata/each.rb, line 42
def each_positional_capture include_names: false, &_b
  return enum_for(:each_positional_capture, include_names: include_names) unless block_given?
  return unless names.empty? || include_names
  captures.each_with_index{|v,i| yield i+1, v }
end
named_captures() click to toggle source

Returns a Hash object of capture name => captured string.

# File lib/mug/matchdata/hash.rb, line 19
def named_captures
  Hash[ names.map{|n| [n, self[n]] } ]
end
positional_captures(include_names: false) click to toggle source

Returns a Hash object of capture position => captured string.

If include_names is given and true, treats named captures as positional captures.

WARNING: if mixing named and positional captures, no positional captures will be available using this method!

# File lib/mug/matchdata/hash.rb, line 31
def positional_captures include_names: false
  return {} unless names.empty? || include_names
  Hash[ captures.each_with_index.map{|v,i| [i+1, v] } ]
end
to_h() click to toggle source

Returns a Hash object of capture position => captured string.

The capture positions are either all Strings or all Integers, depending on whether the original Regexp had named capture groups or not.

# File lib/mug/matchdata/hash.rb, line 9
def to_h
  if names.empty?
    Hash[ captures.each_with_index.map{|v,i| [i+1, v] } ]
  else
    Hash[ names.map{|n| [n, self[n]] } ]
  end
end