Class: DarrylJenks::SubstringFinder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/darryl_jenks/substring_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permutable_key = nil, source_string = nil) ⇒ SubstringFinder

Returns a new instance of SubstringFinder.



7
8
9
10
# File 'lib/darryl_jenks/substring_finder.rb', line 7

def initialize( permutable_key = nil, source_string = nil )
  self.source_string = source_string
  self.permutable_key = permutable_key
end

Instance Attribute Details

#permutable_keyObject

Returns the value of attribute permutable_key.



5
6
7
# File 'lib/darryl_jenks/substring_finder.rb', line 5

def permutable_key
  @permutable_key
end

#source_stringObject

Returns the value of attribute source_string.



5
6
7
# File 'lib/darryl_jenks/substring_finder.rb', line 5

def source_string
  @source_string
end

Instance Method Details

#eachObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/darryl_jenks/substring_finder.rb', line 12

def each
  results = []

  substring_indexes.each do |substring_index|
    results << self.source_string[substring_index..-1]
  end

  if block_given?
    results.each do |result|
      yield result
    end
  else
    return results.to_enum
  end
end

#each_substring(permutable_key) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/darryl_jenks/substring_finder.rb', line 28

def each_substring(permutable_key)
  self.permutable_key = permutable_key

  if block_given?
    each do |result|
      yield result
    end
  else
    return to_a.to_enum
  end
end

#substring_indexesObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/darryl_jenks/substring_finder.rb', line 40

def substring_indexes
  start_index = -1
  string_length = self.source_string.length
  substring_indexes = []

  begin
    break if start_index >= string_length # Issues with JRuby Regex engine pre 1.7.6
    substring_indexes << start_index if start_index > -1
  end while (start_index = self.source_string.index(self.permutable_key, start_index + 1))

  return substring_indexes
end

#to_aObject



53
54
55
# File 'lib/darryl_jenks/substring_finder.rb', line 53

def to_a
  self.each.to_a
end