Research an Enumerable Method

Week 5

Friday, Januaray 07, 2016

Enumerable#map


I decided to research .map. It seems like we will be using .map more than the other two enumerable. The .map will iterate over every object and puts it in a new array. I have an example of how .map works


~> array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Here we have an array of number from one to 10.

~> array.map(&:odd?)
=> [true, false, true, false, true, false, true, false, true, false]
As you can see, ruby iterate over each number and put out each number as a true or false in an array. That is what the .map does.

Here are some other ways you can also add on to the .map method.

~> array.map(&:odd?).select { |odd_numbers| e == true}
=> [true, true, true, true, true]
Here you can use the .map to expand on true and only return true only.

~> array.map(&:odd?).select { |odd_numbers| e == true}.count
=> 5
Here you can see that you can keep expaning on just the .map. You can keep adding to the code as you like because it returns as an array.