title: Read a file in chunks in Ruby
body: <p>I need to read a file in MB chunks, is there a cleaner way to do this in Ruby:</p>
  
  <pre><code>FILENAME="d:\\tmp\\file.bin"
  MEGABYTE = 1024*1024
  size = File.size(FILENAME)
  open(FILENAME, "rb") do |io| 
    read = 0
    while read &lt; size
      left = (size - read)
      cur = left &lt; MEGABYTE ? left : MEGABYTE
      data = io.read(cur)
      read += data.size
      puts "READ #{cur} bytes" #yield data
    end
  end
  </code></pre>