title: Awk-like BEGIN and END with Ruby command-line
body: <p>One of the cool things about Ruby is its ability to behave like typical Unix command-line tools, to do things like (similar to the example from the official documentation):</p>
  
  <pre><code>$ echo "matz" | ruby -pe '$_.upcase!'
  MATZ
  </code></pre>
  
  <p>Awk, on the other hand can perform an aggregation on lines from standard input, e.g., summing a sequence of numbers:</p>
  
  <pre><code>$ for (( i=0; $i &lt; 50; i++ )); do echo $i; done | awk 'BEGIN { tot=0; } { tot += $0 } END { print tot }'
  1225
  </code></pre>
  
  <p>I'd like to know if it's possible to get Ruby to do what is being achieved by the Awk <code>BEGIN</code> and <code>END</code> blocks above so as to be able to do similar aggregation operations.</p>