Ruby is one of the main Object Oriented Programming Languages that is currently out there. Ruby is different from other programming languages in many different ways, one primary way is its naming conventions. Specifically, Ruby has many different names for the same functions, which can prove to be confusing at times, but also can be helpful in bringing in new developers into the coding world and bringing a new sense of creativity to the scene as well.
Ruby is advertised by its founder, Yukihiro Matsumoto, as being a coding language for everyone. One way this is seen is through all of the different names that are given to methods and procedures that functionally perform the same operations.
Map vs. Collect
Ruby offers the user the ability to do a transformation on values of an Array with both Map and Collect. Both of these programmatically perform the same operations. Specifically, Map/Collect take in a procedure and perform that procedure on each element within the array that the method is acted on.
For example:
- Input: [1, 2, 3, 4, 5].map {|element| element + 1}
- Output: => [2, 3, 4, 5, 6]
- Input: [1, 2, 3, 4, 5].collect {|element| element + 1}
- Output: => [2, 3, 4, 5, 6]
As you can see, the array is told to have each element incremented by one, and it then outputs a new array with all of the transformed values.
Reduce vs. Inject
Ruby also offers the user the ability to reduce the entire array to a single value. This can be done through the Reduce method or through the Inject method.
For example:
- Input: [1, 2, 3, 4, 5].reduce {|sum, element| sum + element}
- Output: => 15
- Input: [1, 2, 3, 4, 5].inject {|sum, element| sum + element}
- Output: => 15
In the example, the array is being incremented into one single value. Specifically, it’s iterating through each ‘element’ and adding that element’s value to the ‘sum’, which starts at 0 as a default. In other words, the inputted array is ‘reduced’ down to a single value.
Find_All vs. Select
Lastly, Ruby offers the user the ability to filter out elements in an array to a sub array. For instance, maybe the user wants to see all even values in an array or maybe the users wants to see all of the values greater than a certain number. Both of these instances can be completed with the use of Find_All or with the use of Select.
For example:
- Input: [1, 2, 3, 4, 5].select {|element| element % 2 == 0}
- Output: => [2, 4]
- Input: [1, 2, 3, 4, 5].find_all {|element| element % 2 == 0}
- Output: => [2, 4]
In the example, all of the even numbers are selected out of the array that the method is acting on and they are placed in a separate return array. As you can see, Select and Find_All can be used interchangeably.
As you have learned, Ruby’s founder, Yukihiro Matsumoto, really wants people to use Ruby in their own style, and therefore the language has multiple names for the same command. We learned about Select & Find_All for filtering out values, Map & Collect for transforming arrays, and Reduce & Inject to reduce arrays down to a single output value. This post only covers a few same operations, different name methods. I encourage you to dive into Ruby and use Ruby with your own style.