<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Md. Emran Hasan</title>
    <description>The latest articles on DEV Community by Md. Emran Hasan (@emrancub).</description>
    <link>https://dev.to/emrancub</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F771248%2Febe3711c-4a71-4672-b318-a2645c2ab2b2.jpg</url>
      <title>DEV Community: Md. Emran Hasan</title>
      <link>https://dev.to/emrancub</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emrancub"/>
    <language>en</language>
    <item>
      <title>Mastering Ruby Iterators: Understanding each, collect, select, and map Methods</title>
      <dc:creator>Md. Emran Hasan</dc:creator>
      <pubDate>Fri, 07 Oct 2022 12:24:16 +0000</pubDate>
      <link>https://dev.to/emrancub/confusion-about-each-collect-select-and-map-method-368</link>
      <guid>https://dev.to/emrancub/confusion-about-each-collect-select-and-map-method-368</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;What This Article Covers:&lt;/p&gt;

&lt;p&gt;The differences between each, collect, select, and map methods&lt;br&gt;
Practical examples to clarify how each method works&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The each Method&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [1, 2, 3, 4].each {|x| puts x + 3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4
5
6
7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This block of code adds 3 to each element and prints the result, but it doesn’t change the original array.&lt;/p&gt;

&lt;p&gt;Here’s another example with mixed data types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [1, "Emran", 2, 3, 4].each {|x| puts x.to_s + "Hasan"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1Hasan
EmranHasan
2Hasan
3Hasan
4Hasan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array = [1, "Emran", 2, 3, 4].each {|x| x.to_s + "Hasan"}
puts "array #{array}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array [1, "Emran", 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The collect Method&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The collect method is used when you want to create a new array by modifying each element.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3, 4].collect {|x| puts x * 3 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3
6
9
12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This outputs the results, but the real magic happens when you capture the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_collect = [1, 2, 3, 4].collect {|x| x * 3 }
puts "array_collect #{array_collect}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_collect [3, 6, 9, 12]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, another important array method is &lt;code&gt;select&lt;/code&gt;. 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 &lt;code&gt;select&lt;/code&gt; method. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hinds: &lt;code&gt;select&lt;/code&gt; method used for conditional purpose and this creates a new array.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_select = [1, 2, 3, 4, 6, 8, 9].select do |i|
   i % 2 == 0
end
puts "array_select: #{array_select}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_select: [2, 4, 6, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; i % 2 == 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, it filters the even numbers from the given array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Collect&lt;/code&gt; iterates through an array element by element, and assigns to that element the result of the expression within the code block.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hinds: &lt;code&gt;map&lt;/code&gt; is functionally equivalent to collect.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can access arrays of all the elements by using the loop. Here is a basic loop-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# access element using loop and add a strings
a = [11, 45, "hasan", 46, 'sajon']
i = 0
while (i &amp;lt; a.length)
   puts a[i].to_s + "Amin"
   i += 1
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11Amin
45Amin
hasanAmin
46Amin
sajonAmin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Those two word make change an array: &lt;br&gt;
select!&lt;br&gt;
collect!&lt;br&gt;
map!  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hinds: &lt;code&gt;map&lt;/code&gt; is functionally equivalent to collect. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Important Notes&lt;/p&gt;

&lt;p&gt;The select!, collect!, and map! methods modify the original array in place.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;Happy coding 😀😀😀  &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>array</category>
      <category>rails</category>
      <category>method</category>
    </item>
  </channel>
</rss>
