Ruby — enumerable methods.

The next part of the software engineering bootcamp saw the back end loom into view, for this module, it would be focussed on Ruby. The basics of Ruby are similar to JavaScript in that there are Objects and Arrays, or Hashes as Ruby refers to them.

The enumerable methods were familiar yet different. The three most common methods are #each, #map and #filter. I’ve lifted the images from our coursework as the code-along elements really brought this topic to life.

#each

The #each method is one that can access data in an array and return a value for each element without altering the original array.

code snippet from flatiron.

The code snippet here is an array of drums. The #each method, in this instance, uses the curly parentheses syntax, which is commonly used for single-line blocks. This code outputs a string for each element in the original array that includes element passed down as a #{string literal}. The JavaScript equivalent would be .forEach()

#map

The #map method will map through an original array and return a new array with the same length.

code snippet from flatiron

This snippet contains an array of hashes named daws. The #map method allows for us to return a new array after mapping through the original one. This then utilises #{string literals} again and create new elements for our new array. In this case, we go from an array of 2 hashes to an array of 2 strings. The JavaScript equivalent is .map().

#filter

This also has a couple of built in variable names, #select or #find_all part of the conversational nature of Ruby programming. #filter unsurprisingly, filters an array and return elements based on a given condition.

code snippet from flatiron

This snippet uses #filter on an array of hashes named skills. This array will return a string for with an element that has a key value of true for the log key. We use [:symbols] to select key values in the hashes and a #{string literal} to pass down the key value into the output string. The JavaScript equivalent is .filter().

Syntax elements

Each of these methods have the same building blocks following the same pattern that of name_of_array/hash.ENUMERABLE do | variable | thing_to_do end

What are the || ?

Those are called “pipes”. When invoking an enumerable method like #map, the variable name inside the pipes acts as an argument that is being passed into the block. The method will pass, or yield, each element of the collection on which it is called to the block. Each element, as it gets passed into the block, will be equal to the variable name inside the pipes. Think of it like this:

  • Call, or run, the code in the block once for each element of the collection.
  • Pass a single element of the collection into the block every time the code in the block is called, or run. Start with the first element in the collection, and then move on to the second element, then the third, etc.
  • Every time you call the code in the block and pass in an element from the collection, set the variable name from between the pipes equal to that element.

The value given to the pipes can be anything you want but it should be related to what you are trying to do to keep the code understandable.

--

--

--

Former tech banker, now aspiring software engineer. Feel free to get in touch.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Merge on BigQuery tables with Nested & Repeated fields

KŌSU ⏤ We’ve changed our name.

Java Interview Questions — S01 E01

Build an API using AWS API Gateway & Dell Boomi — Step 1

How to make clickable a part of a text in Android

Industry use cases of Jenkins…

How to Create SQLite Databases in Python

Jemma’s Google Developer Days Diary

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Amersi-Belton

Alex Amersi-Belton

Former tech banker, now aspiring software engineer. Feel free to get in touch.

More from Medium

Integrating Firebase with React(CRUD): No dependency on BackEnd

Handling (d.r.y.) click event

Dependency Injection in Node.js — Higher Order Routing With Express

Dependency Injection in Node.js - Higher Order Routing With Express

Create temporary table in oracle