Skip to content

Instantly share code, notes, and snippets.

@adhusson
Created September 22, 2009 19:08
Show Gist options
  • Save adhusson/191327 to your computer and use it in GitHub Desktop.
Save adhusson/191327 to your computer and use it in GitHub Desktop.
class Array
def each_slice_with_index(slice_size)
nb_slices = (self.size.to_f/slice_size).ceil
for i in 0...nb_slices
yield self[i*slice_size,slice_size], i
end
end
def each_slice_with_index_alt(slice_size)
slices = self.enum_slice(slice_size).map
slices.each_with_index { |slice,index|
yield slice,index
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment