What has been in the works lately? It'll blow your mind.

Generic placeholder image

This stuff is pretty amazing

Enumerable method map 02/25/15

I'd like to tell you a little bit about map, a method that can apply to a list type structure that can be iterated over called an Enumerator. From the Ruby docs we have this:

From Ruby docs: Enumerable

The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. If Enumerable#max, #min, or #sort is used, the objects in the collection must also implement a meaningful <=> operator, as these methods rely on an ordering between members of the collection such as arrays, ranges, hashes, sets.

map { |item| block } → new_ary click to toggle source map → Enumerator Invokes the given block once for each element of self. Creates a new array containing the values returned by the block. See also Enumerable#collect. If no block is given, an Enumerator is returned instead.

I know the above to paragraphs can be a bit confusing because it is written in a very abstract way.

What the above paragraph is saying an Enumerable methods include ways to move across a data structure. These data structures or collections are part of the collection classification of objects including Hashes, Arrays, Lists.

The paragraph also makes reference to a method to compare the elements of the data object. This comparison method is called the combined comparison operator is written as <=>. The comparison operator allows the possibility of having other methods such as sort, min, max. Sorting will sort each element by comparing them in someway and putting them in a specific order. Min will return the minimum of the collection. Max will return the maximum of the collection.

The map method is just a method that performs a specific action on each element of the collection. The argument for map is a code block. Inside the block you can specify what is done to each element of the collection and then a new collection is returned with the block applied to each element.

Examples of using Map

Below we have an example of the use of map on a collection

Arithmetic Operations on Integers

    numbers_array = *(1..10)

    array_add_10 = numbers_array.map { |number| number + 10}

    array_divide_10 = numbers_array.map { |number| number / 10.0}

    array_exponent_10 = numbers_array.map { |number| number ** 10}

    p array_add_10
    p array_divide_10
    p array_exponent_10

The code snippet above shows the map fuction working on the block and below is the ouput

[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
[1, 1024, 59049, 1048576, 9765625, 60466176, 282475249, 1073741824, 3486784401, 10000000000]

class method on Objects

    object_array = ["apple", 1 , [1,2,3]]

    object_class_array = object_array.map { |element| element.class }

    p object_class_array

The code snippet above shows the map function working on the block and the ouptut is shown below:

[String, Fixnum, Array]

These are both great examples of how map can take a block and then change each element of the collection based methods that are specified inside the block