class MatchData
Public Instance Methods
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
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
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
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
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
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
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