Class: BadEncoding::B

Inherits:
Object
  • Object
show all
Defined in:
lib/bad_encoding.rb

Instance Method Summary collapse

Constructor Details

#initialize(segment_size = 100) ⇒ B

Returns a new instance of B.



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

def initialize(segment_size=100)
  @segment_size = segment_size
end

Instance Method Details

#process(s, check) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bad_encoding.rb', line 13

def process(s, check)
  #    puts "size: #{s.size}"
  return s if s.size <= @segment_size

  count = s.size
  middle = count / 2
  end_of_list = count - middle

  first_half = s.slice(0, middle)
  if check.call(first_half)
    return process(first_half, check)
  end

  second_half = s.slice(middle, end_of_list)
  if check.call(second_half)
    return process(second_half, check)
  end

  return 'no bad segments founds'
end

#search(s, &block) ⇒ Object



9
10
11
# File 'lib/bad_encoding.rb', line 9

def search(s, &block)
  process(s, block)
end