Skip to content

Commit

Permalink
updates csv walkthrough to be a more thorough lesson.
Browse files Browse the repository at this point in the history
  • Loading branch information
KatBrandt committed Nov 2, 2023
1 parent 7e2a839 commit 3036a03
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions module1/lessons/csv_walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@ tags: ruby, csv tutorial

### CSV Walkthrough

### Introduction

Think back to your first two projects. How have you currently been making instances of objects in those projects? Likely you have been creating an instance of the objects you need in the `runner` file or having the user input information that was then used to create the object. What if there was a larger set of data that you wanted to use to create objects?

### Using files to create objects

Sometimes the data that we want to use will be stored in a CSV. CSV is a file type that stands for _comma separated values_. Think of it similar to a spreadsheet or an excel sheet. The information within these files can be organized in columns and rows and might look like this:

<img src='./assets/csv_example.png'/>

The first row is the headers for each column which gives us information about the values in the rows below. Each row we can think of as being a package of information that belongs together. Looking at our example we should see the first package of information is `1, Rubeus, Hagrid, 60`. We know that `1` is the id, `Rubeus` is the first name, `Hagrid` is the last name and `60` is the age because of the header for each column in which the information is located.

Rows and columns are nice for us to visually organize information, but what if we want to use that information in our application? Ruby has helped us by having a defined class called CSV. This class contains methods that we can use to interact with a file.

In Ruby, there are classes already defined for us that will allow us to read and write files. Today we are going to focus on the `CSV` class and how to read a file to create instances of our objects. We will be using [this repo](https://github.com/turingschool-examples/csv_example) as an example. Take a moment to look at the files and what they contain.

The `runner.rb` is where we will be writing out code to read our files and create objects. To start
we are going to `require 'CSV'` so that we will have access to its methods. Next, we want to use the `foreach` method from `CSV` and pass it an argument of the file that we want to read. Similar to the `each` enumerable the `foreach` creates a block where the block variable will be a single row in the file. Now add a `pry` within the block so we can see what a row looks like in our block.
The `./lib/runner.rb` file is where we will be writing out code to read our files and create objects. To start, we are going to `require 'CSV'` so that we will have access to its methods. Next, we want to use the `foreach` method from `CSV` and pass it an argument of the file that we want to read. Similar to the `each` enumerable the `foreach` creates a block where the block variable will be a single row in the file. Now add a `pry` within the block so we can see what a row looks like in our block.

```Ruby
require 'CSV'
Expand All @@ -30,6 +43,8 @@ end

Now what does `row` look like in `pry`?

What did these two options accomplish? You can check out the official documentation [here](https://ruby-doc.org/stdlib-3.0.0/libdoc/csv/rdoc/CSV.html#class-CSV-label-Options+for+Parsing) in the Ruby Docs for the CSV library.

These optional arguments help format the row information to be more manageable to work with so that you can create new objects. Now let's create an instance of the `Animal Lovers` class from our data.


Expand All @@ -54,4 +69,5 @@ end

### Practice

On your own, try reading the file and creating magical pet objects.
1. On your own, try reading the file and creating magical pet objects.
1. Head over to the "big repo" and find the `event_manager` directory. It can be found under `../mod-1-be-exercises/lessons/csv_files/event_manager`. Follow the instructions in the `exercise.md` file. The Readme will also provide some additional information.

0 comments on commit 3036a03

Please sign in to comment.