title: Chaining partition, keep_if etc
body: <pre><code>[1,2,3].partition.inject(0) do |acc, x|
    x&gt;2  # this line is intended to be used by `partition`
    acc+=x # this line is intended to be used by `inject`
  end
  </code></pre>
  
  <p>I know that I can write above stanza using different methods but this is not important here.<br>
  What I want to ask why somebody want to use <code>partition</code> (or other methods like <code>keep_if</code>, <code>delete_if</code>) at the beginning of the "chain"?<br>
  In my example, after I chained <code>inject</code> I couldn't use <code>partition</code>. I can write above stanza using <code>each</code>:</p>
  
  <pre><code>[1,2,3].each.inject(0) do |acc, x|
    x&gt;2 # this line is intended to be used by `partition`
    acc+=x # this line is intended to be used by `inject`
  end
  </code></pre>
  
  <p>and it will be the same, right?</p>
  
  <p>I know that <code>x&gt;2</code> will be discarded (and not used) by <code>partition</code>. Only <code>acc+=x</code> will do the job (sum all elements in this case).<br>
  I only wrote that to show my "intention": I want to use <code>partition</code> in the chain like this <code>[].partition.inject(0)</code>.<br>
  I know that above code won't work as I intended and I know that I can chain after block( <code>}.map</code> as mentioned by Neil Slater).   </p>
  
  <p>I wanted to know why, and when <code>partition</code> (and other methods like <code>keep_if</code>, <code>delete_if</code> etc) becomes <code>each</code> (just return elements of the array as <code>partition</code> do in the above cases).<br>
  In my example, <code>partition.inject</code>, <code>partition</code> became <code>each</code> because <code>partition</code> cannot take condition (<code>x&gt;2</code>).<br>
  However <code>partition.with_index</code> (as mentioned by Boris Stitnicky) works (I can partition array and use index for whatever I want): </p>
  
  <pre><code>shuffled_array
    .partition                        
    .with_index { |element, index|    
      element &gt; index                 
  }
  </code></pre>
  
  <p>ps. This is not question about how to get sum of elements that are bigger than 2.</p>