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
12
13
14
# File 'lib/regexp-examples/groups.rb', line 7

def initialize(result, group_id = nil, subgroups = [])
  @group_id = group_id
  @subgroups = subgroups
  if result.respond_to?(:group_id)
    @subgroups = result.all_subgroups
  end
  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



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

def all_subgroups
  [self, subgroups].flatten.reject { |subgroup| subgroup.group_id.nil? }
end