Git Product home page Git Product logo

ruby-array-methods's Introduction

General Assembly Logo

Ruby Array Methods (versus JavaScript)

We'll explore Ruby's Array type and some of its methods. We'll also have a look at a few ways in which Ruby makes list processing (iterating through arrays) a bit more fun for the developer.

List processing is at the core of most web development tasks, so practicing it in Ruby (and later, comparing it to JavaScript), will illuminate the logical features of list processing tasks in addition to implementation specifics.

Prerequisites

Objectives

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

  • Use Ruby array methods to iterate through arrays.
  • Pass inline code as blocks to Array instance methods.
  • Pass method names as symbols to Array instance methods.

Preparation

  1. Fork and clone this repository.
  2. Make a new branch, training.
  3. Install dependencies with bundle install.

Array Methods

Both JavaScript and Ruby have types that represent lists. In both languages, these types are called "arrays". In Ruby, Array is a class that holds methods for arrays, and in JavaScript, Array.prototype is an object that holds methods for arrays.

Let's compare the list of methods for Array in each language. Here is some documentation for reference:

Demo: map

Let's explore Ruby's map method.

# in irb or pry
fibs = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

How can we use map to multiply each member of fibs by two in Ruby?

# fibs.map { |fib| ... } for single-line, or

fibs.map do |fib|
  # something here
end

Code Along: reduce

Let's use reduce to calculate the sum of elements in fibs.

In JavaScript, we used a function to control the behavior of Array methods. In Ruby, we use a block ( { |p| ... } for one line blocks, do ... end for multi-line blocks).

Lab: Fibonacci Calculations

In Ruby, write scripts, in lib/fibonacci.rb.

  • calculates the product of elements of fibs (excluding zero).
  • calculates the sum of the odd elements of fibs
  • calculates the product of the even elements of fibs(excluding zero).

Code-Along: People

Let's use the script bin/people_array.rb to explore Array methods in Ruby. The data in the objects we'll be processing comes from the comma separated values (CSV) file data/people.csv.

The Person objects we'll test against have properties/methods that align with the headers in data/people.csv plus the method age.

Lab: People

Use the bin/people_array.rb script to

  • Count all the people who are older than you (or just pick an age).
  • Count all the people who are younger than you (or just pick an age).
  • Count all the people whose first name and last name start with the same letter.
  • Calculate the average age of all the people.

Code-Along: Set Operations

The results of the Ruby Array methods -, &, |, and flatten aren't easily reproducible in JavaScript.

We'll explore these methods using bin/sets_etc.rb.

Bonus Challenge: Return of the Word Count

Remember our text analysis exercise from way-back?

Let's implement normalize_words, unique_words, word_count, and word_frequencies in lib/string.rb

  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-array-methods's People

Contributors

raq929 avatar laurenfazah 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.