Jan Dennison

map! Wait

Jan 18, 2015

They don't love you like I love you

Recently we learned a lot about Ruby and working with methods and classes. One method that I didn't really understand and thus avoided using it in my challenges was map. I've been introduced to the concept in both Ruby and JavaScript. I vaguely understood it to mean that, while I was looping through an array1, I would push the value of the current array1[index] to a new array2[same index]. I didn't really understand, so I found myself asking things like, "What's the difference between map and push? Between map and clone? Between map and dup?" - but that was before I read this blog article, which made map, click!

In the blog article, Eric Trautman explains clearly and simply not just what methods are but how they relate together. Now, I understand map because I have context as to why I would use and how it relates to similar methods, such as each, select, and collect. At the same time, I understood how they can help me save lines of code as well! Let's take an example of how I would have normally set up a situation:


    my_array1 = ["Ohio","Florida","California","Washington", "Massachusettes"]
    def my_method(my_array1)
      my_array2 = []
      for i in my_array
        if i = "California" || i = "Washington" || i = "Massachusettes"
          my_array2.push(i)
        end
      end
      return my_array
    end
  

Yep, that's newbie code right there. And it works, which is what we want code to do. I'm saying "Take this list of states I've lived in, and if any entries match my two favorites, push it to a new list of states I want to revisit." But! That's a lot of lines of code for what I really need to do. That's where other methods come in!

each is a familiar method that takes an array or hash, and iterates through each item. You can do some things to that item, like print it, but then it goes away unless you save it to a new array by setting up a variable with an empty array to hold it, which is adding lines of code.


      def my_method(my_array1)
        my_array1.each {|state| puts state }
        # => returns my_array1 unchanged
      end
    

select will fit your need if you want to pull out an item from your original array, and it creates a new array to return, so your original can remain the same. But, select will only choose items that pass as true, and sometimes we need to work with the entire set.


      def my_method(my_array1)
        my_array1.select {|state| state == fav_states }
        # => returns a new array of fav states
      end
    

collect is the next logical need, when you need to make a new array from the old and also modify it while doing so! It returns a new array so you don't need to set up an empty variable and push to it. And here's the best part - map is the same thing as collect, so now we know what map does!


      def my_method(my_array1)
        my_array1.map {|state| state = "California!" }
        # => returns a new array, only now every state I have lived in says "California!"
      end