Ruby-on-rails – the second parameter/argument to CSV.open( ) in ruby

rubyruby-on-rails

I think I'm missing something really obvious here, but what is the second argument that everyone puts in for CSV.open method, in this case its 'wb', I've seen other letter(s) put here, but no one really explains what it does. What does it do?

CSV.open("path/to/file.csv", "wb") do |csv|
  csv << ["row", "of", "CSV", "data"]
  csv << ["another", "row"]
  # ...
end

The ruby doc doesn't seem to give any explanation. http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html

Thanks!

Best Answer

From the IO Open Mode documentation:

"r" Read-only, starts at beginning of file (default mode).

"r+" Read-write, starts at beginning of file.

"w" Write-only, truncates existing file to zero length or creates a new file for writing.

"w+" Read-write, truncates existing file to zero length or creates a new file for reading and writing.

"a" Write-only, starts at end of file if file exists, otherwise creates a new file for writing.

"a+" Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing.

Related Topic