Here’s a quick one to paginate an Array in Ruby. This little Array monkey patch makes it easy to paginate an array of pretty much anything.
class Array def paginate(page=1, per_page=10) each_slice(per_page).to_a[page-1] end def pages(per_page=10) (count / per_page.to_f).ceil end end
We use the paginate method directly on the array we want to paginate, along with the page argument (which ‘chunk’ of array elements we want returning) and the per_page argument (how many elements there are per ‘chunk’). These are already set at sensible defaults.
When paginate is called it uses the Enumerable method each slice to break the array into chunks, each containing x amount of elements, defined by per_page. The returned Enumerable is then converted to an array with to_a before the required ‘chunk’ from this array is returned (page).
pages is even easier. It divides the amount of elements in the array by the amount we want per ‘chunk’. We need to convert per_page to a float (to_f) so the result is returned as a decimal. This is rounded up with ceil and that’s it. We now know how many ‘chunks’ or pages the pagination will create. All good gen.
That’s the boring how it works crap out of the way. Now for the boring let’s see it in action crap. It’s all pretty self explanatory.
array = (1..42).to_a #=> [1,2,3,4,5 ... 39,40,41,42] array.pages #=> 5 array.pages(5) #=> 9 array.pages(50) #=> 1 array.paginate #=> [1,2,3,4,5,6,7,8,9,10] array.paginate(3) #=> [21,22,23,24,25,26,27,28,29,30] array.paginate(3,5) #=> [11,12,13,14,15]