Class: MatchData
- Defined in:
- lib/mug/matchdata/each.rb,
lib/mug/matchdata/hash.rb
Instance Method Summary collapse
-
#each(&b) ⇒ Object
Iterates over each capture group in the MatchData object, including $& (the entire matched string), yielding the captured string.
-
#each_capture(&b) ⇒ Object
Iterates over each capture group in the MatchData object, yielding the capture position and captured string.
-
#each_named_capture(&b) ⇒ Object
Iterates over each named capture group in the MatchData object, yielding the capture name and string.
-
#each_positional_capture(include_names: false, &b) ⇒ Object
Iterates over each positional capture group in the MatchData object, yielding the capture position and string.
-
#named_captures ⇒ Object
Returns a Hash object of capture name => captured string.
-
#positional_captures(include_names: false) ⇒ Object
Returns a Hash object of capture position => captured string.
-
#to_h ⇒ Object
Returns a Hash object of capture position => captured string.
Instance Method Details
#each(&b) ⇒ Object
Iterates over each capture group in the MatchData object, including $& (the entire matched string), yielding the captured string.
7 8 9 10 |
# 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(&b) ⇒ Object
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.
18 19 20 21 22 23 24 25 |
# 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(&b) ⇒ Object
Iterates over each named capture group in the MatchData object, yielding the capture name and string.
29 30 31 32 |
# 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, &b) ⇒ Object
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!
42 43 44 45 46 |
# 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 ⇒ Object
Returns a Hash object of capture name => captured string.
18 19 20 |
# File 'lib/mug/matchdata/hash.rb', line 18 def named_captures Hash[ names.map{|n| [n, self[n]] } ] end |
#positional_captures(include_names: false) ⇒ Object
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 Hash[ captures.each_with_index.map{|v,i| [i+1, v] } ] end |
#to_h ⇒ Object
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.
9 10 11 12 13 14 15 |
# 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 |