If you don t know how the ruby file class works.
Ruby read csv file line by line.
Reading csv file in ruby.
Ruby comes with a built in csv library.
Require csv customers csv read customers csv.
Here is the process in detail.
Read method and parse the file name as argument.
Read the file the whole file line by line or a specific amount of bytes.
Csv read method to read the complete file.
You can read a file in ruby like this.
If it was a simpler file we could read it line by line and use split to cut it into parts but in this file there is a field that has a comman in it.
Open the file with the open method.
This method will read the entire file and store it in the variable.
Close the file with the close method.
You can read a file directly.
You open a csv file for read write or append mode and use either or puts to append a new line.
Require csv csv read favorite foods csv or you can parse a string with csv.
This is what a csv file looks like.
Ruby comes with a standard library called csv to make it easy to read files with comman separated values.
See the example below.
Use the file class to open a file.
Ruby stores each table row as an array with each cell as a string element of the array.
How to read files in ruby.
Ruby provides csv support in the standard library and it will help us do most of the job very quickly.