ga-wdi-boston / ruby-vs-js-array-methods

Other
0 stars 44 forks source link

General Assembly Logo

Ruby Array Methods (versus JavaScript)

We'll contrast Ruby's Array methods with JavaScript's.

Prerequisites

Objectives

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

Preparation

  1. Fork and clone this repository.
  2. Make a new branch, training.
  3. Install dependencies with npm install and 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

Ruby and JavaScript Arrays both have a map and a reduce method. Let's explore using them to see the similarities and differences.

# in irb or pry
fibs = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
// in node
let 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 both Ruby and JavaScript?

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

fibs.map do |fib|
  # something here
end
fibs.map((fib) => {
  // something here
});

Code Along: reduce

Let's use reduce to calculate the sum of elements in fibs in both Ruby and JavaScript.

In JavaScript, we use 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

Alternating between Ruby and JavaScript, write scripts, in bin/fibonacci.rb and bin/fibonacci.js, that:

Code-Along: People

Let's use the scripts bin/people-array.js and bin/people_array.rb to explore Array methods in both Ruby and JavaScript. 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.* scripts to

Lab: Compare and Contrast Methods

Build a table mapping Ruby Array methods to direct or modified invocations of JavaScript Array methods. Show the invocation, on an array variable named a, with an example block or function. For the JavaScript equivalent of Ruby Array's +, use an array variable named b.

Ruby JavaScript
<method> every
<method> filter
<method> find
map map
reduce reduce
<method> some
+ <method>

What do you notice?

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

License

  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 legal@ga.co.