Git Product home page Git Product logo

ruby-enumerable-io's Introduction

General Assembly Logo

Ruby Enumerable IO

Prerequisites

Objectives

By the end of this lesson, developers should be able to:

  • Iterate through a file one line at a time.
  • Explain why you should only use the block form of File.open.
  • Load data using the CSV library in order to create Ruby objects.

Preparation

  1. Fork and clone this repository. FAQ
  2. Create a new branch, training, for your work.
  3. Checkout to the training branch.
  4. Install dependencies with bundle install.

Introduction

In Ruby, files, and all IO streams, are Enumerable. You're familiar with files and folders. Examples of IO streams, other than files, are stdin, stdout, and stderror.

Files as lists

Ruby's File type mixes-in Enumerable via its parent class, IO). Therefore, we can use all of the Enumerable methods to process files. That means, to Ruby at least, files are just lists, and we can process them in chunks, either a character or a line at a time. By default, Ruby will process files one line at a time.

Other enumerable classes related to working with files include IO (mentioned above) and Dir. Dir is Ruby's abstraction for working with directory structures.

Code along - read a file

Using bin/read_file.rb we'll read all the lines in a file and print them.

Lab - count characters, words, and lines in a file

Let's create a script to mimic the behavior of the wc (word count) command line utility in bin/word_count.rb.

Files in the CSV format

A file containing Comma Separated Values (CSV) is a simple and well supported format for data interchange, especially for tabular data. It is an open format describing plain text (data) separated by commas. It may or may not have a "header" as the first line, describing "columns" of data. Each piece of data is represented as a "row".

Demo - Inspect a CSV file

Tabular data, columns, and rows? We're dealing with a spreadsheet (without formulas). There is an example in data/people.csv.

  1. Watch as I open the file in my text editor.
  2. Watch as I open the file in a spreadsheet program.

Demo - CSV class

We previously used the Ruby class CSV to load data for us. The CSV class is part of the standard library, which means that we can use it without downloading a gem.

We used CSV for the bin/people_array.rb script in Ruby Array Methods.

  1. Watch as I run bin/people_array.rb.
  2. Note how I inspect the files associated with the script, specifically looking for how the CSV class is used.

Code along - CSV

We'll build a data loader for pets in lib/pets.rb using CSV.

We'll use a lambda to ensure we use properly formatted symbols as keys when loading data. We'll use the shorthand syntax, sometimes called the "stabby" lambda, ->([args]) {[code]}.

A lambda in Ruby is the closest thing to an anonymous function that we have. Ruby lambdas verify arity do not stop execution during their return; they are different from Procs in that regard (Proc docs).

Instead of using a lambda to convert our headers, we could pass a symbol from HeaderConverters as the value for :header_converters in the options Hash.

Challenge - Read two files simultaneously

Read two files at the same time using bin/read_files.rb.

Look at Enumerator which is what gets returned when we call each on an open file without a block.

We'll need to look briefly at exception handling as Enumerator relies on this mechanism.

Challenge - Annotate unfamiliar code

  1. Annotate lib/people.rb if you haven't already.
  2. Annotate lib/person.rb. Start with annotating what you can easily annotate. Then, methodically and actively reading the difficult portions, annotate each remaining line.
  3. Annotate lib/pets.rb if you haven't already.
  4. Annotate lib/pets.rb. Start with annotating what you can easily annotate. Then, methodically and actively reading the difficult portions, annotate each remaining line.
  5. Compare and contrast the person files as a group with the pet files as a group.

Tasks

Developers should run these often!

  • bin/rake nag (or bundle exec rake nag): runs code quality analysis tools on your code and complains.
  • bin/rake test (or bundle exec rake test): runs automated tests.
  • bin/rake will run both nag and test
  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

ruby-enumerable-io's People

Contributors

payne-chris-r avatar jrhorn424 avatar micfin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.