Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the second parameter/argument to CSV.open( ) in ruby?

Tags:

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!

like image 364
StickMaNX Avatar asked Jul 25 '13 18:07

StickMaNX


People also ask

How do I create a CSV file in rails?

First, set up the application and seed in some data. Now, in post. rb , declare a method which will be responsible for generating data in CSV format. Depending upon your Rails version and the dependencies added, it's possible you'll need to add a require statement.


1 Answers

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.

like image 54
James Avatar answered Sep 22 '22 02:09

James