Consider using (also in other places):
File.open('foo', 'w') do |f|
    f.write "bar"
end