Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/job_dollars/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#in_groups_of(number, fill_with = nil) ⇒ Object

File activesupport/lib/active_support/core_ext/array/grouping.rb, line 20



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/job_dollars/core_ext/array.rb', line 3

def in_groups_of(number, fill_with = nil)
  if number.to_i <= 0
    raise ArgumentError,
      "Group size must be a positive integer, was #{number.inspect}"
  end

  if fill_with == false
    collection = self
  else
    padding = (number - size % number) % number
    collection = dup.concat(Array.new(padding, fill_with))
  end

  if block_given?
    collection.each_slice(number) { |slice| yield(slice) }
  else
    collection.each_slice(number).to_a
  end
end