The operator x = File.open(...) do |f| ... x = File.open(...) do |f| ... sets x to the result of the do block, and not to the returned file (which closes after the block finishes). So what you are effectively doing is setting temp_image to the result of f.write , which is the number of bytes written, not the file.
source share