Class: Array

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

Overview

I JUST REALIZED I ALREADY USED .scan(/../) TO GET .groups_of(2) AND DUUUUH .scan() IS BASICALLY ALREADY .groups_of() TODO: get rid of it?! update: no .scan(/../) works on strings and groups_of works on arrays so it has its purpose

Instance Method Summary collapse

Instance Method Details

#groups_of(max_size) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/array.rb', line 12

def groups_of(max_size)
  return [] if max_size < 1

  groups = []
  group = []
  each do |item|
    group.push(item)

    if group.size >= max_size
      groups.push(group)
      group = []
    end
  end
  groups.push(group) unless group.size.zero?
  groups
end