As I began my programming journey, I encountered several challenges with Ruby's each, collect, select, and map methods. To help fellow beginners, I’ve decided to write this article based on my experiences during my internship. Understanding these methods is crucial for iterating through arrays and processing data efficiently in Ruby.
What This Article Covers:
The differences between each, collect, select, and map methods
Practical examples to clarify how each method works
- The each Method
The each method iterates over each element in the array and passes it to a block of code. It’s straightforward for processing arrays without modifying them.
Example:
[1, 2, 3, 4].each {|x| puts x + 3}
Output:
4
5
6
7
This block of code adds 3 to each element and prints the result, but it doesn’t change the original array.
Here’s another example with mixed data types:
[1, "Emran", 2, 3, 4].each {|x| puts x.to_s + "Hasan"}
Output:
1Hasan
EmranHasan
2Hasan
3Hasan
4Hasan
Notice how it treats every element, regardless of type, and appends "Hasan" to it. However, the array itself remains unchanged after the block is executed:
array = [1, "Emran", 2, 3, 4].each {|x| x.to_s + "Hasan"}
puts "array #{array}"
Output:
array [1, "Emran", 2, 3, 4]
- The collect Method
The collect method is used when you want to create a new array by modifying each element.
Example:
[1, 2, 3, 4].collect {|x| puts x * 3 }
Output:
3
6
9
12
This outputs the results, but the real magic happens when you capture the result:
array_collect = [1, 2, 3, 4].collect {|x| x * 3 }
puts "array_collect #{array_collect}"
Output:
array_collect [3, 6, 9, 12]
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}"
Output:
array_select: [2, 4, 6, 8]
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
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
Output:
11Amin
45Amin
hasanAmin
46Amin
sajonAmin
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.
Important Notes
The select!, collect!, and map! methods modify the original array in place.
Conclusion
This is my first article aimed at beginners learning Ruby. I hope it clears up the confusion surrounding these array methods. If you find any errors or have suggestions, feel free to share your feedback. Let’s keep learning and coding together!
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)