Class: Array

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

Instance Method Summary collapse

Instance Method Details

#in_groups_of(n) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/core_ext.rb', line 27

def in_groups_of(n)
  raise ArgumentError, "Data is not in multiples of #{n}" unless size % n == 0
  
  inject([[]]) { |a,e| (a.last.size == n) ? (a << [e]) : (a.last << e);  a }
end