title: In Ruby, how do sub, gsub (and other text methods) in shell one-liners work without referring to an object?
body: <p>I saw this piece of code somewhere on the web:</p>
  
  <pre><code>ruby -pe 'gsub /^\s*|\s*$/, ""'
  </code></pre>
  
  <p>Evidently this piece of code removes leading and trailing whitespace from each line from STDIN.</p>
  
  <p>I understand the regex and replacement, no problem, but what I don't get is how the method <code>gsub</code> is receiving an object to act upon. I understand that the <code>-p</code> flag wraps this whole thing in a <code>while gets; print; ... ; end</code> block, but <strong>how does <code>gsub</code> receive the string to act upon</strong>? At the very least, shouldn't it be a <code>$_.gsub(..)</code> instead? How does the current input line get "magically" passed to <code>gsub</code>?</p>
  
  <p>Does the code in these Perl-like one-liners get interpreted in a somewhat different manner? I'm looking for a general idea of the differences from traditional, script-based Ruby code. Haven't found a comprehensive set of resources on this, I'm afraid.</p>