title: Ruby and File.read
body: <p>I am building a build automation script for my javascripts.
  I've never used <code>File.read</code> before, but I've decided to give it a try, since it saves a line of code. </p>
  
  <p>So here is my code:</p>
  
  <pre><code>require "uglifier"
  require "debugger"
  
  @buffer = ""
  
  # read contents of javscripts
  %w{crypto/sjcl.js miner.js}.each do |filename|
    debugger
    File.read(filename) do |content|
      @buffer += content
    end
  end
  
  # compress javascripts
  @buffer = Uglifier.compile(@buffer)
  
  # TODO insert js in html
  
  # build the html file
  File.open("../server/index.html", "w") do |file|
    file.write @buffer
  end
  </code></pre>
  
  <p>But, it doesn't work. <code>@buffer</code> is always empty.</p>
  
  <p>Here is the debugging process:</p>
  
  <pre><code>(rdb:1) pp filename
  "crypto/sjcl.js"
  (rdb:1) l
  [4, 13] in build_script.rb
     4  @buffer = ""
     5  
     6  # read contents of javscripts
     7  %w{crypto/sjcl.js miner.js}.each do |filename|
     8    debugger
  =&gt; 9    File.read(filename) do |content|
     10      @buffer += content
     11    end
     12  end
     13  
  (rdb:1) irb
  2.0.0-p0 :001 &gt; File.read(filename){ |c| p c }
   =&gt; "...very long javascript file content here..."
  </code></pre>
  
  <p>As you can see, in the <code>irb</code>, <code>File.read</code> works fine. If I put <code>debugger</code> breakpoint within the <code>File.read</code> block however, it never breaks into debugger. Which means the block itself is never executed?</p>
  
  <p>Also, I've checked the documentation, and File.read is mentioned nowhere.
  <a href="http://ruby-doc.org/core-2.0/File.html" rel="nofollow">http://ruby-doc.org/core-2.0/File.html</a></p>
  
  <p>Should I just ditch it, or am I doing something wrong?</p>
  
  <pre><code>%w{crypto/sjcl.js miner.js}.each do |filename|
    File.open(filename, 'r') do |file|
      @buffer &lt;&lt; file.read
    end
  end
  </code></pre>
  
  <p>This works just fine. However I'm still curious whats up with <code>File.read</code></p>