Evolve from .each with these helpful enumerables
Tags: ruby, beginners, tutorial
Published: 2019-10-02 14:17:21


![Ruby](https://cdn.emojidex.com/emoji/px32/Ruby.png "Ruby")<strong><center><h1>`|map|`</h1></center></strong>
***



Ruby's `#map` is an extremely useful upgrade from using `#each`. Not only is it less code to write but it also looks much cleaner. Check out these examples of what `#map` can do in the place of `#each`.
```ruby
  rainbow = ["red", "orange", "yellow", "green", "blue", "purple"]
  
  colors = []
  rainbow.each{ |color| colors << color.upcase }
  colors
  # returns ["RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "PURPLE"]


  rainbow.map{ |color| color.upcase }
  # returns ["RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "PURPLE"]
```
Pretty cool right! Ruby's `#map` returns a new `array` without changing the original one. If you were to use `#each` you would need to create an empty `array` to `shovel` or `push` the altered values into. You can use map to grab all of the things! 


![guy saying map all the things](https://thepracticaldev.s3.amazonaws.com/i/r6vsmcn8zbh927cm1lug.jpg)
***




![Ruby](https://cdn.emojidex.com/emoji/px32/Ruby.png "Ruby")<strong><center><h1>`|select|`</h1></center></strong>
***
What if you just wanted to grab specific things instead of mapping all of the things? Well you actually can with `#select`!

```ruby
   colors = []
   rainbow.each do |color| 
     if color.length > 5
       colors << color
     end
   end
   colors
   # returns ["orange", "yellow", "purple"]


   rainbow.select{ |color| color.length > 5 }
   # returns ["orange", "yellow", "purple"]  
```
>In this example we only grab colors with a length greater than 5. When using `#select` a new array will be created and populated with the values we specify.
***





![Ruby](https://cdn.emojidex.com/emoji/px32/Ruby.png "Ruby")<strong><center><h1>`|max_by|`</h1></center></strong>
***

What if you want to grab the largest thing? You can use `#max_by` to grab the max value of something.
```ruby
   rainbow = ["red", "orange", "yellow", "green", "blue", "purpleee"]

   longest = ""
   rainbow.each do |color|
     if color.length > longest.length
       longest = color
     end
   end
   longest
   # returns "purpleee"
   

   rainbow.max_by{ |color| color.length }
   # returns "purpleee"
```
You can also specify how many values you want returned. The values returned are similar to using `#sample` so the order will be random.
```ruby
   rainbow = ["red", "orange", "yellow", "green", "blue", "purple"]

   colors = []
   longest = ""
   second_longest = ""
   third_longest = ""
   rainbow.each do |color|
     if color.length >= longest.length
       third_longest = second_longest
       second_longest = longest
       longest = color
     end
   end
    colors.push(longest, second_longest, third_longest)
    colors
    # returns ["purple", "orange", "yellow"]
   

   rainbow.max_by(3){ |color| color.length }
   # returns ["purple", "orange", "yellow"]
```
>If our array looked like this `["red", "orange", "yellow", "greenn", "blue", "purple"]` then any combination of 3 of these `["yellow", "greenn", "orange", "purple"]` could have been returned.
***



![Ruby](https://cdn.emojidex.com/emoji/px32/Ruby.png "Ruby")<strong><center><h1>`|include?|`</h1></center></strong>
***
What if we wanted to find out if our rainbow array has a certain color? We can do that by using `#include?`.

```ruby
   rainbow.each do |color| 
     if color == "purple" 
       true
     else
       false
     end
   end
   # returns true

   
   rainbow.include?("purple")
   # returns true

   rainbow.include?("pink")
   # returns false
```
>Keep in mind that `include?` returns true or false and not a new array.
***






<strong><h1>`The magic of & with enumerables.`</h1></strong>
We can even use this shorthand with enumerables when we don't need to pass in any arguments. This really blew my mind when I found out about it.
```ruby
   rainbow.map(&:upcase)
   # take note that the usual squigglys{} are now parentheses().
   # returns ["RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "PURPLE"]
```
<p><a href="https://media.giphy.com/media/wxw2e19ZMsjio/giphy.gif"><img src="https://media.giphy.com/media/wxw2e19ZMsjio/giphy.gif" alt=""></a></p>