Last active
July 29, 2017 23:26
-
-
Save knightsamar/7e917dc04a095ab04f79e6ef38f2648d to your computer and use it in GitHub Desktop.
.reduce(:+) explanation
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
#thanks to https://flats.github.io/blog/2015/12/02/getting-to-know-rubys-map-and-reduce/ | |
pry(main)> x = [1,2,3] | |
=> [1, 2, 3] | |
pry(main)> y = [2,5,6] | |
=> [2, 5, 6] | |
pry(main)> z = [42,41,5] | |
=> [42, 41, 5] | |
pry(main)> n = [x,y,z] | |
=> [[1, 2, 3], [2, 5, 6], [42, 41, 5]] | |
pry(main)> n.reduce(:+) | |
=> [1, 2, 3, 2, 5, 6, 42, 41, 5] | |
pry(main)> n.reduce(:+).uniq | |
=> [1, 2, 3, 5, 6, 42, 41] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment