Class: Range

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

Instance Method Summary collapse

Instance Method Details

#split(params = {}) ⇒ Object



2
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
31
32
33
# File 'lib/range_splitter.rb', line 2

def split(params = {})
  into = params[:into] || 2
  endianness = params[:endianness] || :big

  unless [:big, :little].include?(endianness)
    err = 'The endianness parameter must be either :big or :little'
    raise ArgumentError.new(err)
  end

  if into <= 0
    err = "Cannot split #{self} into #{into} ranges."
    raise ArgumentError.new(err)
  end

  if into == 1
    [self]
  else
    partition = min + (count.to_f / into).ceil - 1

    if max == partition
      [self]
    else
      args = params.merge(:into => into - 1)
      partition -= 1 if endianness == :little

      head = min..partition
      tail = ((partition + 1)..max).split(args)

      [head] + tail
    end
  end
end