Class: Auxilium::IntegerChunker

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

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ IntegerChunker

Returns a new instance of IntegerChunker.



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

def initialize(number)
  @number = number
end

Instance Method Details

#chunks_of(chunk_size, random: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/auxilium/integer_chunker.rb', line 9

def chunks_of(chunk_size, random: false)
  number = @number
  chunks = []

  while number > 0
    this_chunk_size = random ? rand(chunk_size - 1) + 1 : chunk_size
    number -= this_chunk_size

    if number < 0
      this_chunk_size += number
      number = 0
    end

    chunks << this_chunk_size
  end

  chunks
end