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
.
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!
What if you just wanted to grab specific things instead of mapping all of the things? Well you actually can with #select
!
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.
What if you want to grab the largest thing? You can use #max_by
to grab the max value of something.
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.
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.
What if we wanted to find out if our rainbow array has a certain color? We can do that by using #include?
.
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.
The magic of & with enumerables.
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.
rainbow.map(&:upcase)
# take note that the usual squigglys{} are now parentheses().
# returns ["RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "PURPLE"]
Top comments (0)