Take care when writing to files using puts
February 5, 2010 § Leave a comment
For writing text files; especially line by line, it’s perfectly acceptable to use puts. But, be warned that this will append a line break after the data being written. This behaviour is not desirable when working with binary files, when IO::write is a better choice to avoid unwanted line break characters.
file = File.new('temp.file', 'w+') file.sync = true while data = get_data file.write data end file.rewind
Original post: Take care when writing to files using puts.