Class: MatchData
- Defined in:
- lib/mug/matchdata/each.rb,
lib/mug/matchdata/hash.rb
Instance Method Summary collapse
-
#each {|str| ... } ⇒ Enumerator, MatchData
Iterates over each capture group in the MatchData object, including
$&(the entire matched string), yielding the captured string. -
#each_capture {|key, str| ... } ⇒ Enumerator, MatchData
Iterates over each capture group in the MatchData object, yielding the capture position and captured string.
-
#each_named_capture {|name, str| ... } ⇒ Enumerator, MatchData
Iterates over each named capture group in the MatchData object, yielding the capture name and string.
-
#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.
-
#positional_captures(include_names: false) ⇒ Hash{Integer => String}
Returns a Hash object of capture position => captured string.
-
#to_h ⇒ Hash{String => String}, Hash{Integer => String}
Returns a Hash object of capture position => captured string.
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.
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.
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.
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!
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!
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_h ⇒ Hash{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.
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 |