Class: Array

### Home Classes Methods

### In Files

- lib/will_paginate/array.rb

### Parent

Object

### Methods

- #paginate

### Class/Module Index

Quicksearch

- WillPaginate
- WillPaginate::ActionView
- WillPaginate::ActionView::LinkRenderer
- WillPaginate::ActiveRecord
- WillPaginate::ActiveRecord::BaseMethods
- WillPaginate::ActiveRecord::Pagination
- WillPaginate::ActiveRecord::RelationMethods
- WillPaginate::Collection
- WillPaginate::CollectionMethods
- WillPaginate::DataMapper
- WillPaginate::DataMapper::CollectionMethods
- WillPaginate::DataMapper::Pagination
- WillPaginate::Deprecation
- WillPaginate::Deprecation::Hash
- WillPaginate::I18n
- WillPaginate::InvalidPage
- WillPaginate::Merb
- WillPaginate::Merb::LinkRenderer
- WillPaginate::PageNumber
- WillPaginate::PerPage
- WillPaginate::PerPage::Inheritance
- WillPaginate::Railtie
- WillPaginate::Railtie::ControllerRescuePatch
- WillPaginate::Railtie::ShowExceptionsPatch
- WillPaginate::SequelMethods
- WillPaginate::Sinatra
- WillPaginate::Sinatra::Helpers
- WillPaginate::Sinatra::LinkRenderer
- WillPaginate::ViewHelpers
- WillPaginate::ViewHelpers::LinkRenderer
- WillPaginate::ViewHelpers::LinkRendererBase
- Array
- Object

No matching classes.

# Array

### Public Instance Methods

paginate(options = {}) click to toggle source

Paginates a static array (extracting a subset of it). The result is a WillPaginate::Collection instance, which is an array with a few more properties about its paginated state.

Parameters:

- `:page` - current page, defaults to 1

- `:per_page` - limit of items per page, defaults to 30

- `:total_entries` - total number of items in the array, defaults to `array.length` (obviously)

Example:

    arr = ['a', 'b', 'c', 'd', 'e']
    paged = arr.paginate(:per_page => 2)      #->  ['a', 'b']
    paged.total_entries                       #->  5
    arr.paginate(:page => 2, :per_page => 2)  #->  ['c', 'd']
    arr.paginate(:page => 3, :per_page => 2)  #->  ['e']

This method was originally suggested by Desi McAdam and later proved to be the most useful method of will_paginate library.

    # File lib/will_paginate/array.rb, line 24
    def paginate(options = {})
      page     = options[:page] || 1
      per_page = options[:per_page] || WillPaginate.per_page
      total    = options[:total_entries] || self.length

      WillPaginate::Collection.create(page, per_page, total) do |pager|
        pager.replace self[pager.offset, pager.per_page].to_a
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.