Giter Club home page Giter Club logo

ruby-enumerable-io's Introduction

General Assembly Logo

Ruby Enumerables

Instructions

Fork, clone, branch (training), and bundle install.

Objectives

By the end of this lesson, students 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.

Introduction

In Ruby, files, and all IO streams, are Enumerable.

Files as lists

Ruby's File includes Enumerable so we can use all of its methods to process files a character or a line (the default) at a time.

Other enumerable classes related to working with files include IO, and Dir.

I used the Ruby Standard Library class CSV to load data for the bin/*_array.rb scripts.

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.

CSV files

A file containing Comma Separated Values (CSV) is a simple and well supported format for data interchange, especially for tabular data.

Code along - CSV

We'll build a data loader for pets in lib/pets.rb using the Ruby standard library class CSV.

We'll use a lambda - shorthand syntax ->([args]) {[code]}, see Proc - to ensure we use symbols as keys when loading data. In Ruby, lambdas verify the number of arguments.

Challenge

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.

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

ruby-enumerable-io's People

Watchers

James Cloos avatar Sean Faulkner 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.