title: Ruby partially retrieve large amount of records and iterate over them
body: <p>I'm newbie in Ruby but I have a lot of experience in other programming languages. I need to iterate over large amount of records (from db or any persistent storage). Storage engine allows me to retrieve records partially by ranges. In PHP I usually write custom iterator that loads range of records iterate over them and when need loads next part of records and forget about previous part. Some trade-off between script memory usage and count of request to storage. Something like this (copied from comments <a href="http://php.net/manual/en/class.iterator.php" rel="nofollow noreferrer">here</a>):</p>
  <pre><code>class Database_Result_Iterator {
  ...
  private $_db_resource = null;
  private $_loaded = false;
  private $_valid = false;
  
  function rewind() {
      if ($this-&gt;_db_resource) {
          mysql_free($this-&gt;_db_resource);
          $this-&gt;_db_resource = null;
      }
      $this-&gt;_loaded = false;
      $this-&gt;_valid = false;
  }
  
  function valid() {
      if ($this-&gt;_loaded) {
          $this-&gt;load();
      }
      return $this-&gt;_valid;
  }
  
  private function load() {
      $this-&gt;_db_resource = mysql_query(...);
      $this-&gt;_loaded = true;
      $this-&gt;next(); // Sets _valid
  }
  </code></pre>
  <p>}</p>
  <p>How such approach is transformed in Ruby? I.e. I have some class <em>Voter</em> and method <strong>get_votes</strong> that returns all <em>votes</em> belong to current voter object. It is possible to retrieve not an array with all votes but collection of votes with possibility to iterate over it. How should I implement it?</p>
  <p><strong>UPDATE</strong></p>
  <p>Please not consider ActiveRecord and RDBMS as only one possible storage. And what about Redis as storage and commands like LRANGE? I'm interested in common code pattern for solution such kind of problem in Ruby.</p>