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.
For example:

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.

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Take care when writing to files using puts at Fringley's Blog.

meta

%d bloggers like this: