<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: paginate code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 07 Oct 2008 08:30:19 GMT</pubDate>
    <description>DZone Snippets: paginate code</description>
    <item>
      <title>Paginate over a collection (Array or Hash)</title>
      <link>http://snippets.dzone.com/posts/show/3364</link>
      <description>Sometimes could happen that we need to paginate something different from a collection, I found this very useful method that acts similar to a paginate but it works for Arrays ( and Hashes )&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def paginate_collection(collection, options = {})&lt;br /&gt;    default_options = {:per_page =&gt; 10, :page =&gt; 1}&lt;br /&gt;    options = default_options.merge options&lt;br /&gt;&lt;br /&gt;    pages = Paginator.new self, collection.size, options[:per_page], options[:page]&lt;br /&gt;    first = pages.current.offset&lt;br /&gt;    last = [first + options[:per_page], collection.size].min&lt;br /&gt;    slice = collection[first...last]&lt;br /&gt;    return [pages, slice]&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 25 Jan 2007 17:59:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3364</guid>
      <author>Mickael (Mickael)</author>
    </item>
  </channel>
</rss>
