DEV Community

Joseph Louie
Joseph Louie

Posted on • Updated on

Ruby Map Ruby each, map, reduce, unless, and until

When I started programming I had no idea what enumerables/conditionals were in ruby. Now that I've been coding in ruby for a little while I've used some enumerables/conditionals more than others. In this post, I'll explain some of the enumerables/conditionals you might see as your time as a software engineer.

.each

.each is a common method that you might see in other people's code. The .each method goes through all the elements in an array or a hash. Here are some examples of how I would use .each method.

This first example demonstrates how you would use .each for multiple lines.
Example:

array = [1, 2, 3]
array.each do |element|
  puts element
end
Enter fullscreen mode Exit fullscreen mode

The above will give us

1
2
3
Enter fullscreen mode Exit fullscreen mode

Another way to write a .each method is by using {}. Using {} generally means that you are going to put all your logic in one line.

array = [1, 2, 3]
array.each { |element| puts element }
Enter fullscreen mode Exit fullscreen mode
1
2
3
Enter fullscreen mode Exit fullscreen mode

Heres an example of using .each with a hash

hash = {
  dog: 1,
  cat: 2
}
hash.each do |key, value|
  puts "#{key} #{value}"
end
Enter fullscreen mode Exit fullscreen mode
dog 1
cat 2
Enter fullscreen mode Exit fullscreen mode

.map

.map is one of those methods where if you're thinking about using .each think about if you can use .map instead. I know when I was starting to program I would always use .each because I felt comfortable with it. Sometimes it's good to get out your comfort zone.

I mainly use .map for arrays. What .map does is it takes in an array, does some type of logic, and outputs a new array. This is beneficial because you don't have to create a new array variable to store your modified elements. Another great thing about .map is that you won't mutate your original array!
Example:

array = [1, 2, 3]
new_array = array.map { |element| element * 2 }
p array
p new_array
Enter fullscreen mode Exit fullscreen mode

Above I'm doing a one-line example because we don't too much logic.
new_array = array.map { |element| element * 2 } goes through my array and multiplies each element by 2 and assigns them to a new array.

[1, 2, 3] #original array
[2, 4, 6] #new array
Enter fullscreen mode Exit fullscreen mode

.map is definitly one of those methods you will want to know.

.reduce

.reduce is another useful method that allows you to accumulate all the numbers in an array and return a single number. There are more use cases for .reduce but I mainly use it when I'm adding a lot of numbers and just want a total.
Example:

array = [1, 2, 3, 4, 5]
array.reduce { |sum, n| sum + n }
Enter fullscreen mode Exit fullscreen mode

The above is only one of many ways to use/write .reduce. Please refer to here for more detail on how you can fully utilize this method.

15
Enter fullscreen mode Exit fullscreen mode

What the .reduce method is doing is taking each element in the array and adding it to sum until we have finished going through our entire array. You can specify what sum is initially but here sum would be our first element, which is 1.

unless

These next two conditionals are something I rarely use but is nice to know that I have them if I need them. Unless is the opposite of if.

total = 2
if total < 10
  puts "total is less than 10"
end
Enter fullscreen mode Exit fullscreen mode
total = 2
unless total > 10
  puts "total is less than 10"
end

Enter fullscreen mode Exit fullscreen mode

Both puts:

total is less than 10
Enter fullscreen mode Exit fullscreen mode

until

Another conditional I rarely use because is it just the opposite of a while loop. Go do something until this condition is true.

total = 2
while total != 5 # while total is not equal to 5
  puts total
  total += 1 #add 1 to total
end
Enter fullscreen mode Exit fullscreen mode
total = 2
until total == 5 # until total is equal to 5
  puts total
  total += 1 #add 1 to total
end
Enter fullscreen mode Exit fullscreen mode

Both puts:

2
3
4
Enter fullscreen mode Exit fullscreen mode

Conculsion

There are many methods and conditional in Ruby or any other language. You don't have to use them all. Familiarize yourself with the most common ones and looks up what you don't remember. Each, Map, and reduce are good to practice. Unless and until aren't needed as much because we have if and while, but you have options available to you.

Thank You for reading! If you have any suggestions or want to point out a mistake I'll try to update accordingly.

Top comments (0)