Reading And Writing Outside Your Ruby Script

There are many ways to access things outside your script, so today we will review three classes(modules?) to access and bring in these foreign objects into your script. gets.chomp is/will be in another blog post.

CSV

CSV means comma seperated values.

When you insert a CSV file into a ruby script, you get an array of strings for each line from the csv file. String elements of your arrays are delineated by a comma from your original CSV file. In order to use these commands, you need to require 'csv'in your ruby script. Use these CSV methods only on a CSV file.

Reading CSV Files Into Your Script

All CSV commands are used upon this particular CSV file:

CSV.open("filename") returns a CSV object

CSV.open("filename").to_a changes your CSV object into a parent array. You can then view the nested arrays that were hidden from your view when your parent array was a CSV object.

CSV.read("filename") is the same thing as CSV.open("filename").to_a It will return nested arrays of string elements within one parent array.(strings in nested arrays are seperated by commas from original CSV file.)

CSV.foreach("filename"){|row| #do w/e with row} reads CSV file line by line and returns an array of strings for each line. It does not return a parent array like CSV.read

You can include a parameter for headers(which will change your nested arrays to hashes with first line in your CSV file as your keys.) and then call information as so:

CSV File:

Ruby File:

Writing to your CSV File

Use an argument "wb" in conjunction with CSV.open to write over your entire csv file..

Corresponding CSV file

Use argument "a+" in conjunction with CSV.open to write at the end of your csv file.

Corresponding CSV file

More on CSV commands

File

File allows you to access any type of file to read or write into your script. The output of using File depends on what File method you useFile is a child class of IO, so you can use IO methods with File.

We will be referencing this file(example.txt) for each File method.

File.open("filename"returns a file object

File.read("filename")opens your external file and inputs entire file as 1 string into your ruby script

File.readlines("filename")opens your file and inputs entire file as one array of string elements. You can method-chain .each to access each string element to do w/e you wnat to the string elements. I believe string elemtns are set by each new line in the external file.

ARGV

ARGV takes input from the terminal concurrently as you run your script and inserts it as one array with strings into your ruby script. String elemetns in array are separated by spaces from user input in the terminal.

Be careful when using ARGV with gets.chomp in the same ruby script! If you do so, you will get an error as ARGV is always listening for user input while gets.chomp will wait until script is run and then prompt for user input. ARGV and gets.chomp are listening from the same STDIN(Standard Input) array. One way to bypass this error is using:

to create a new STDIN array that gets.chomp would pull from, or

to save and remove the ARGV input from the original STDIN array.

More info on ARGV and gets.chomp error