Take care when writing to files using puts

[tweetmeme service=bit.ly]
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.

For example:

file = File.new('temp.file', 'w+')
file.sync = true

while data = get_data
  file.write data

Posted in Ruby. Tags: , , , . 1 Comment »