Ruby is a relatively young language. One of the advantages with this is that Ruby has libraries for common programming needs. In this post, I will show you how easy it is to process CSV files in Ruby.
A CSV file contains comma separated values and is an easy way to store information in text files. In many Web applications, CSV file is generated and is output as excel filetype so that Microsoft excel program can process it.
For this example, let us consider the following CSV file. It contains exam results of 3 students.
Save this as examresults.txt. Following program shows how this can be read using Ruby. In this example, we will read the exam results, print the name of the people who failed the exam and finally add one more entry to the exam results file. All this can be achieved with minimal plumbing code!
Check out the documentation for complete set of methods available in csv library!