title: Ruby: Combining enumerators, making a new enumerator
body: <p>I needed a function which is similar to <code>Array#select</code>, but passes to the block not only the data item, but also the index of the element (similar to <code>Enumerable#each_with_index</code>).  I tried this:</p>
  
  <pre><code>class Array  
    def select_with_index
      self.each_with_index.select {|*args| yield(*args)}.map(&amp;:first)
    end
  end
  </code></pre>
  
  <p>This seems to work indeed:</p>
  
  <pre><code>['a','b','a','a','c'].select_with_index {|letter,index| letter == 'a' &amp;&amp; index &gt; 1 }
  </code></pre>
  
  <p>results in</p>
  
  <pre><code>["a", "a"]
  </code></pre>
  
  <p>as expected. However, what I don't like with my solution is, that one <strong>has</strong> to supply a block. Similar methods in the Ruby core can be called without a block and yield an <code>Enumerator</code>. How can I do this? I know that I can use <code>block_given?</code> to test for the presence of the block, but how do I continue then? Do I need a <code>Fiber</code>?</p>
  
  <p>BTW, the code should work with both Ruby 1.9.3. and 2.x.</p>