Module: RangesMergerDivider

Included in:
RangesMerger
Defined in:
lib/ranges_merger/ranges_merger_divider.rb

Instance Method Summary collapse

Instance Method Details

#divide(_array, interval, allow_partials = true) ⇒ Object

Divide not merged array



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ranges_merger/ranges_merger_divider.rb', line 3

def divide(_array, interval, allow_partials = true)
  divided_array = Array.new
  joined_array = merge(_array)

  joined_array.each do |r|
    r_from = r[0]
    r_to = r[1]
    r_now = r_from

    while r_now <= r_to
      new_r_from = r_now
      new_r_to = new_r_from + interval

      if allow_partials and new_r_to > r_to and new_r_from < r_to
        new_r_to = r_to
      end

      divided_array << [new_r_from, new_r_to] if new_r_to <= r_to
      r_now += interval
    end



  end

  return divided_array

end