Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/skates/ext/array.rb
Instance Method Summary collapse
Instance Method Details
#in_groups_of(number, fill_with = nil) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/skates/ext/array.rb', line 2 def in_groups_of(number, fill_with = nil) if fill_with == false collection = self else # size % number gives how many extra we have; # subtracting from number gives how many to add; # modulo number ensures we don't add group of just fill. padding = (number - size % number) % number collection = dup.concat([fill_with] * padding) end if block_given? collection.each_slice(number) { |slice| yield(slice) } else returning [] do |groups| collection.each_slice(number) { |group| groups << group } end end end |