The Last Enumerable

The Last Enumerable

Enumerables in Ruby can be extremely powerful, and can turn a messy loop into a few lines of clean and efficient code. However, given the…

Enumerables in Ruby can be extremely powerful, and can turn a messy loop into a few lines of clean and efficient code. However, given the vast number of methods available, it can be difficult to determine which one will work best for you. In this post, we’ll examine ten useful enumerable methods with which you should be able to overcome many of your coding challenges.

Setting Up Your Enumerables Using do .. end or { … }

When coding in Ruby, you have the option to set up your enumerables using either do … end, or using curly brackets. Using the do … end method is considered a little easier to read, especially for beginner coders, while the curly brackets option will give you a cleaner look. As we’ll be using the latter option in our examples below, we’ll quickly remind ourselves of how one method translates to the other.

Option 1: do … end Method

Character.all.find_all do |character|
     character.name == “Katara”
end

Option 2: Curly Brackets Method

Character.all.find_all {|character| character.name == “Katara” } 

The first option is very similar to the second, with the key differences being that the opening bracket is replaced with do, the closing bracket is replaced with end, and the code is spread out over 3 lines rather than 1. Either method is completely acceptable, so use whichever you feel most comfortable with.

10 Essential Enumerables

We can now take a look at the ten enumerable method every coder should have in their back pocket. To demonstrate how each method can be used, we’ll be working with data based on characters from the tv series, Avatar: The Last Airbender. For our examples, we’ve created a Character class, where each character has several attributes, including a name, age, home nation, special skill, and role.

1. map

When coding in Ruby, map will probably become one of your most commonly used enumerable methods. It iterates over each element, and returns a new array containing the results of that iteration. It is especially advantageous because it can be used it in conjunction with other enumerable methods, as we’ll see in some of the examples below.

Our Avatar characters have a variety of different special skills. In this example, we’re interested in retrieving a list of those skills using map.

def self.all_skills
    skills = all.map { |character| character.special_skill }
end

Character.all_skills
=> [“Airbending”, “Firebending”, “Earthbending”, “Waterbending”, “Firebending”, “Hook Swords”, “Waterbending”, “Knives”, “Firebending”, “Boomerang”, “Fans”, “Earthbending”, “Chi Blocking”, “Waterbending”, “Firebending”]

We now have an array containing all of the special skills mastered by our characters. However, several of our characters share the same skills, and some of the elements are therefore repeated. We can easily solve this by adding a .uniq, which will ensure that each skill only appears once in our array.

def self.all_skills
     skills = all.map { |character| character.special_skill }.uniq
end
Character.all_skills
=> [“Airbending”, “Firebending”, “Earthbending”, “Waterbending”, “Hook Swords”, “Knives”, “Boomerang”, “Fans”, “Chi Blocking”]

Great! We now have an array of unique elements.

ruby flatiron-school enumerable coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Day 14 of 30 Ruby Coding Challenge - Fibonacci Sequence the Ruby Way

Day 14 of 30. We're going to solve the famous Fibonacci sequence by taking advantage of features that Ruby offers.

How Benchmarking Your Code Will Improve Your Ruby Skills

Learning to code is a path full of struggles, and learning Ruby isn't the exception. But you'll agree with me, that practice is the best way to learn and develop your skills.

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

Day 18 of 30 Ruby Coding Challenge - Finding the Missing Number Game

We're going to play a game: find the missing number in a given array. This first solution will be not that great but we'll get the job done!

Day 16 of 30 Ruby Coding Challenge - Sum Even Numbers

Day 16 of 30 - Ruby Coding Challenges in 30 Days. We're going to sum all the even numbers in a given Fibonacci sequence.This is the blog post version of the Youtube video from the 30 Ruby Coding Challenges in 30 Days series