Class: RegexpExamples::BaseRepeater

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group) ⇒ BaseRepeater

Returns a new instance of BaseRepeater.



4
5
6
# File 'lib/regexp-examples/repeaters.rb', line 4

def initialize(group)
  @group = group
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



3
4
5
# File 'lib/regexp-examples/repeaters.rb', line 3

def group
  @group
end

#max_repeatsObject (readonly)

Returns the value of attribute max_repeats.



3
4
5
# File 'lib/regexp-examples/repeaters.rb', line 3

def max_repeats
  @max_repeats
end

#min_repeatsObject (readonly)

Returns the value of attribute min_repeats.



3
4
5
# File 'lib/regexp-examples/repeaters.rb', line 3

def min_repeats
  @min_repeats
end

Instance Method Details

#random_resultObject



23
24
25
26
27
28
# File 'lib/regexp-examples/repeaters.rb', line 23

def random_result
  result = []
  rand(min_repeats..max_repeats).times { result << group.random_result }
  result << [ GroupResult.new('') ] if result.empty? # in case of 0.times
  RegexpExamples::permutations_of_strings(result)
end

#resultObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/regexp-examples/repeaters.rb', line 8

def result
  group_results = group.result[0 .. RegexpExamples.MaxGroupResults-1]
  results = []
  min_repeats.upto(max_repeats) do |repeats|
    if repeats.zero?
      results << [ GroupResult.new('') ]
    else
      results << RegexpExamples.permutations_of_strings(
        [group_results] * repeats
      )
    end
  end
  results.flatten.uniq
end