 <p>How about:</p> <pre><code>lines = out.lines do_something_1(lines.first) lines[1..-2].each do |line| do_something_2(line) end do_something_3(lines.last) </code></pre> <p>This code will do some action on the first line, some other action on all lines except the first and the last, and a third action on the last line.</p>