title: Implicit return values in Ruby
body: <p>I am somewhat new to Ruby and although I find it to be a very intuitive language I am  having some difficulty understanding how implicit return values behave.</p>
  
  <p>I am working on a small program to grep Tomcat logs and generate pipe-delimited CSV files from the pertinent data. Here is a simplified example that I'm using to generate the lines from a log entry.</p>
  
  <pre><code>class LineMatcher
    class &lt;&lt; self
      def match(line, regex)
        output = ""
        line.scan(regex).each do |matched|
          output &lt;&lt; matched.join("|") &lt;&lt; "\n"
        end
        return output
      end        
    end
  end
  
  
  puts LineMatcher.match("00:00:13,207 06/18 INFO  stateLogger - TerminationRequest[accountId=AccountId@66679198[accountNumber=0951714636005,srNumber=20]",
                         /^(\d{2}:\d{2}:\d{2},\d{3}).*?(\d{2}\/\d{2}).*?\[accountNumber=(\d*?),srNumber=(\d*?)\]/)
  </code></pre>
  
  <p>When I run this code I get back the following, which is what is expected when explicitly returning the value of output.</p>
  
  <pre><code>00:00:13,207|06/18|0951714636005|20
  </code></pre>
  
  <p>However, if I change LineMatcher to the following and don't explicitly return output:</p>
  
  <pre><code>    class LineMatcher
        class &lt;&lt; self
          def match(line, regex)
            output = ""
            line.scan(regex).each do |matched|
              output &lt;&lt; matched.join("|") &lt;&lt; "\n"
            end
          end        
        end
      end
  </code></pre>
  
  <p>Then I get the following result:</p>
  
  <pre><code>00:00:13,207
  06/18
  0951714636005
  20
  </code></pre>
  
  <p>Obviously, this is not the desired outcome. It feels like I should be able to get rid of the output variable, but it's unclear where the return value is coming from. Also, any other suggestions/improvements for readability are welcome.</p>