Class: MatchData

Inherits:
Object show all
Defined in:
lib/mug/matchdata/each.rb,
lib/mug/matchdata/hash.rb

Instance Method Summary collapse

Instance Method Details

#each {|str| ... } ⇒ Enumerator, MatchData

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

Yields:

  • (str)

    each captured string

Yield Parameters:

  • str (String)

    the captured string

Returns:



12
13
14
15
# File 'lib/mug/matchdata/each.rb', line 12

def each &_b
  return enum_for(:each) unless block_given?
  to_a.each{|v| yield v }
end

#each_capture {|key, str| ... } ⇒ Enumerator, MatchData

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.

Yields:

  • (key, str)

    each capture position and string

Yield Parameters:

Returns:



29
30
31
32
33
34
35
36
# File 'lib/mug/matchdata/each.rb', line 29

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 {|name, str| ... } ⇒ Enumerator, MatchData

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

Yields:

  • (name, str)

    each capture name and string

Yield Parameters:

  • name (String)

    the capture name

  • str (String)

    the captured string

Returns:



46
47
48
49
# File 'lib/mug/matchdata/each.rb', line 46

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) {|pos, str| ... } ⇒ Enumerator, ...

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!

Parameters:

  • include_names (Boolean) (defaults to: false)

    whether to treat named captures as positional

Yields:

  • (pos, str)

    each capture position and string

Yield Parameters:

  • pos (Integer)

    the capture position

  • str (String)

    the captured string

Returns:

  • (Enumerator)

    if no block is given

  • (nil, MatchData)

    nil if named captures exist and include_names is false



66
67
68
69
70
# File 'lib/mug/matchdata/each.rb', line 66

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

#positional_captures(include_names: false) ⇒ Hash{Integer => String}

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!

Parameters:

  • include_names (Boolean) (defaults to: false)

    whether to treat named captures as positional

Returns:



29
30
31
32
# File 'lib/mug/matchdata/hash.rb', line 29

def positional_captures include_names: false
  return {} unless names.empty? || include_names
  captures.each_with_index.to_h{|v,i| [i+1, v] }
end

#to_hHash{String => String}, Hash{Integer => String}

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.

Returns:



11
12
13
14
15
16
17
# File 'lib/mug/matchdata/hash.rb', line 11

def to_h
  if names.empty?
    captures.each_with_index.to_h{|v,i| [i+1, v] }
  else
    names.to_h{|n| [n, self[n]] }
  end
end