DEV Community

Cover image for Understanding Iterations & Enumerators
Lupita Rivera
Lupita Rivera

Posted on • Edited on

1

Understanding Iterations & Enumerators

Staring my first CLI project I notice I was falling behind on iterations which made things hard to even get my code started. follow and join me on my journey of going back to review the lessons of iteration and Enumerators in ruby.

Starting with the word iteration itself - means that is a way to operate on a collection object, like an array, and do something with each element in that collection. I notice there are different types of iterations and Enumerators for example; EACH, COLLECT/MAP, FIND/DETECT FIND_ALL/SELECT, let's go over these iterations, and Enumerators find out how they operate.

Starting with Each, this iterator is just another method of an object. the main feature is that iterates on each element of the collection always returning the original collection. you can use it also on an array, hashes, and range. see below example,

[ 1, 2, 3 ].each { |n| puts n }
1
2
3
=> [1, 2, 3]

The next iteration that will be looking at is Collect/Map these two iterations are aliases that can be used interchangeably. It passes each collection item to the block and returns a new collection containing the values returned by the block. see below example,

a = [ "lydia", "Anthony", "mike" ]

a.map { |x| x.upcase }

=> ["LYDIA", "ANTHONY", "MIKE"]

Now let's take a look at the next ones, select/find_all these methods will not do calculations on collection, but it will allow us to select specific elements from the base collection based on logical conditions. If an element of the collection will fulfill the condition, then it will stay in the collection. If no, element will be removed from the collection. see below example

       array = [1,2,3,4,5,6]
Enter fullscreen mode Exit fullscreen mode

array.select {|num| num.even?} => [2,4,6]
array.find_all {|num| num.even?} => [2,4,6]

This Enumerator is very useful when you want to choose only a specific group of elements. You are not interested in all collection, but with a small part of it. but only when it comes to arrays, we can use .select and .find_all interchangeably.

Alt Text

but When it comes to iterating over hashes, this is when we see the difference between .select and .find_all.

   hash= {a:1,b:2,c:3,d:4,e:5,f:6}
Enter fullscreen mode Exit fullscreen mode

hash.select {|key,value| value.even?}
=> {:b=>2, :d=>4, :f=>6}

hash.find_all {|key,value| value.even?}
=>[[:b, 2],[:d, 4],[:f, 6]]

Notice in the above example how .select returns the value in the original input in this case a hash. but .find_all return the key: value pairing in an array. so .find_all output is always an array.

So far we already know how iterations and Enumerator work! having this understanding help me figure out how I was going to manipulate my data that I scraped on an API. and gave my project a solid ground to rise up. Thank you for following throughout on this journey now we have an understanding of iterations and Enumerators.

Alt Text

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay