“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay who are based in Denmark...”

Rob Lacey
Senior Software Engineer, Copenhagen, Denmark

Paginated Array

Old blog posts I never published, except I have now #8

Came across a situation whereby we expected

Artist.where(:a => 'b').paginate(:page => 1)
class PaginatedArray < Array

  attr_reader :current_page, :per_page, :total_entries

  def initialize(values = [], options = {})
    @current_page = options[:page] || 1
    @per_page = options[:per_page] || 20
    @total_entries = values.size

    start = (@current_page - 1) * @per_page
    finish = start + (@per_page - 1)

    super(values[start, finish])
  end

end
def paginate(options = {}) PaginatedArray.new(self, options) end
Artist.where(:a => 'b').paginate(:page => 1)
Artist.where(:a => 'b').to_a.paginate(:page => 1)