2022-11-05 16:48:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-29 10:09:10 +00:00
|
|
|
class Array
|
|
|
|
def groups_of(max_size)
|
|
|
|
return [] if max_size < 1
|
|
|
|
|
|
|
|
groups = []
|
|
|
|
group = []
|
2022-11-05 16:19:05 +00:00
|
|
|
each do |item|
|
2022-10-29 10:09:10 +00:00
|
|
|
group.push(item)
|
|
|
|
|
|
|
|
if group.size >= max_size
|
|
|
|
groups.push(group)
|
|
|
|
group = []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
groups.push(group) unless group.size.zero?
|
|
|
|
groups
|
|
|
|
end
|
|
|
|
end
|