Method: File.writelines

Defined in:
lib/core/facets/file/writelines.rb

.writelines(path, data) ⇒ Object

Writes the given array of data to the given path and closes the file. This is done in binary mode, complementing IO.readlines in standard Ruby.

Note that readlines (the standard Ruby method) returns an array of lines with newlines intact, whereas writelines uses puts, and so appends newlines if necessary. In this small way, readlines and writelines are not exact opposites.

data = ['The content', ['for the file']]
File.writelines('writelines.txt', data)

Returns number of lines written.

CREDIT: Noah Gibbs, Gavin Sinclair



19
20
21
22
23
24
# File 'lib/core/facets/file/writelines.rb', line 19

def self.writelines(path, data)
  File.open(path, "wb") do |file|
    file.puts(data)
  end
  data.size
end