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

The Last Enumerable
1.40 GEEK