DEV Community

Cover image for Confusion about each, collect, select and map method
Md. Emran Hasan
Md. Emran Hasan

Posted on

Confusion about each, collect, select and map method

At an early age of the job life, when I learn Ruby programming language I face some problems with each, collect, select, and map method. So, I write this article for beginner label programmers. In the beginning, many programmers had confusion about each, collect, select, and map method. That's why I write this article for me and others so that they can be benefited from this. I am using the ruby programming language. This note I made when I am doing an internship.

This article will cover:

Confusion about each, collect, select and map method

Iterating through arrays is simple and uses each method. Theeach method goes through each element of the array and passes it as a parameter to the code block we supply.

 [1, 2, 3, 4].each {|x| puts x + 3}
Enter fullscreen mode Exit fullscreen mode

Output:

4
5
6
7
Enter fullscreen mode Exit fullscreen mode

It is only valid for the block.

 [1, "Emran", 2, 3, 4].each {|x| puts x.to_s + "Hasan"}
Enter fullscreen mode Exit fullscreen mode

Output:

1Hasan
EmranHasan
2Hasan
3Hasan
4Hasan
Enter fullscreen mode Exit fullscreen mode

But when puts the same things after the block. This time that will print the original values.

array = [1, "Emran", 2, 3, 4].each {|x| x.to_s + "Hasan"}
puts "array #{array}"
Enter fullscreen mode Exit fullscreen mode

Output:

array [1, "Emran", 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

We can also convert an array on the fly using the collect method.

[1, 2, 3, 4].collect {|x| puts x * 3 }
Enter fullscreen mode Exit fullscreen mode

Output:

3
6
9
12
Enter fullscreen mode Exit fullscreen mode

But after the block if we puts the array value then it changes and print the new array.

array_collect = [1, 2, 3, 4].collect {|x| x * 3 }
puts "array_collect #{array_collect}"
Enter fullscreen mode Exit fullscreen mode

Output:

array_collect [3, 6, 9, 12]
Enter fullscreen mode Exit fullscreen mode

And, another important array method is select. Select method creates a new array based on the conditions. Suppose an array has many elements like even numbers and odd numbers. But we need to print only even numbers or odd numbers. For these situations we need to use the select method.

Hinds: select method used for conditional purpose and this creates a new array.

array_select = [1, 2, 3, 4, 6, 8, 9].select do |i|
   i % 2 == 0
end
puts "array_select: #{array_select}"
Enter fullscreen mode Exit fullscreen mode

Output:

array_select: [2, 4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

Here, we print only the even numbers but our array has even and odd numbers. So, we need to print only even numbers and that's why we use select method and in the block we used odd numbers conditions.

 i % 2 == 0
Enter fullscreen mode Exit fullscreen mode

Finally, it filters the even numbers from the given array.

Collect iterates through an array element by element, and assigns to that element the result of the expression within the code block.

Hinds: map is functionally equivalent to collect.

We can access arrays of all the elements by using the loop. Here is a basic loop-

# access element using loop and add a strings
a = [11, 45, "hasan", 46, 'sajon']
i = 0
while (i < a.length)
   puts a[i].to_s + "Amin"
   i += 1
end
Enter fullscreen mode Exit fullscreen mode

Output:

11Amin
45Amin
hasanAmin
46Amin
sajonAmin
Enter fullscreen mode Exit fullscreen mode

This works in a similar way to each method. It should be immediately apparent to anyone why iterators, code blocks, and methods such as each and collect are preferable with ruby, as they make the code significantly easier to read and understand.

Those two word make change an array:
select!
collect!
map!

Hinds: map is functionally equivalent to collect.

Thank you, keep your eyes for more practice. This is my first article. Please feel free to share your thoughts regarding it. I will try to modify it if make any mistakes. Please forgive me if I make any mistakes.

Happy coding 😀😀😀

Top comments (0)