Created
August 24, 2012 15:44
-
-
Save karatedog/3452159 to your computer and use it in GitHub Desktop.
Ruby Array's subtract method which subtracts the values of the elements not the elements themselves
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby Array's "-" (minus) method removes elements from the receiver which exist in the parameter Array | |
[5, 6, 1] - [2, 3, 5] | |
# => [6, 1] | |
# this #subtract method will subtract the value of the parameter Array from the value of the receiver Array, defined by the actual index. | |
# type mismatch, Nil, and different Array length are not handled | |
class Array | |
def subtract(other_ary) | |
self.map.with_index {|v, i| v-other_ary[i]} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment