Class: RegexpExamples::GroupResult

Inherits:
String
  • Object
show all
Defined in:
lib/regexp-examples/groups.rb

Overview

All Group#result methods return an array of GroupResult objects The key objective here is to keep track of all capture groups, in order to fill in backreferences

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, group_id = nil, subgroups = []) ⇒ GroupResult

Returns a new instance of GroupResult.



7
8
9
10
11
# File 'lib/regexp-examples/groups.rb', line 7

def initialize(result, group_id = nil, subgroups = [])
  @group_id = group_id
  @subgroups = result.respond_to?(:group_id) ? result.all_subgroups : subgroups
  super(result)
end

Instance Attribute Details

#group_idObject (readonly)

Returns the value of attribute group_id.



6
7
8
# File 'lib/regexp-examples/groups.rb', line 6

def group_id
  @group_id
end

#subgroupsObject (readonly)

Returns the value of attribute subgroups.



6
7
8
# File 'lib/regexp-examples/groups.rb', line 6

def subgroups
  @subgroups
end

Instance Method Details

#all_subgroupsObject



13
14
15
# File 'lib/regexp-examples/groups.rb', line 13

def all_subgroups
  [self, subgroups].flatten.keep_if(&:group_id)
end

#swapcaseObject



17
18
19
20
# File 'lib/regexp-examples/groups.rb', line 17

def swapcase
  # Override to preserve subgroups
  GroupResult.new(super.to_s, group_id, subgroups)
end