What is this map function and how do I use it?
A Week 4 Reflection
May 9, 2015
A Module
in Ruby is a collection of Methods
and Constraints
. Think about modules as "libraries" which group together methods, classes and contraints. Methods are about functions, and you can use them across multiple items like classes. Classes on the otherhand, are more about "objects." Today, we'll dive into the Enumerabe
module. The special features of the Enumerable
module is that it provides a set of methods that traverse, search, sort and manipulate collections.
Plain English please? Let's walk through some examples
Let's say you have an array and you'd like to make some changes to it.
array = [1,2,3]
If you'd like to double all the elements in the array how could you do that? Well, the Enumerable
module has a special method called map
that let's you do just that.
Before we get into it, let's talk about a few other keywords. A "block" is a chunk of code overwhich you'd run a method. To identify the beginning of a block you'd use the Ruby keyword do
. The method Each
belongs to the array class and is an iterator. Which means that it will cycle through blocks fo code and will perform the the action requested on that element in the collection. Evey block of code that uses Each
needs to close with an end
so that Ruby knows to stop iterating.
Back to the doe. Let's say we want to double everything. We could do the following
array = [1,2,3,4]
array.each do |element|
p element * 2
end
This produces the following result:
2
4
6
8
And then if we ask for the array. It will show:
array = [1,2,3,4]
The original array has not been modified.
What if we want to transform the array but maintain it as an array rather than as individual outputs? We could use the map function.
array = [1,2,3,4]
new_array = array.map { |element| element*2 }
p new_array # => [2,4,6,8]
p array # => [1,2,3,4]
array = [1,2,3,4]
array.map! { |element| element*2 }
p array # => [2,4,6,8]
Using a destructive method !
helps us "re-write" the array itself and redefines it.
meals = ["breakfast", "lunch", "dinner"]
The array because the array tracks the order of the items, we know that breakfast is the first object, lunch is the second, and dinner is the third. If we wanted to call the second item in the meals
array, we could call it by writing meals[0]
. The first item in the array is always called via a 0. The next item is a 1, so meals[1]
would return lunch
.
In contrast, hashes do not necessarily have to be ordered. In fact, it was not possible to order a hash until more recent releases of Ruby. Hashes store objects in pairs, where each pair has a key and a value. Whereas, the ingredients in a fruit salad could be a hash. Each ingredient has it's own "size/amount" that needs to go in the salad, but it doesn't matter which order the items are assembled. Take for instance the following hash:
tasty_fruit_salad = { "10" => "strawberries", "40" => "blueberries", "2" => apples}
The great benefit of a hash is that I don't need to remember which "order" the apples are in. I can find the number of apples I need by calling apples in the hash: tasty_fruit_salad[apples]
which will return 2
.
Arrays and hashes are both powerful ways to store and manage data, and each has their own particular use cases.