Skip to content

Instantly share code, notes, and snippets.

@knightsamar
Last active July 29, 2017 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knightsamar/7e917dc04a095ab04f79e6ef38f2648d to your computer and use it in GitHub Desktop.
Save knightsamar/7e917dc04a095ab04f79e6ef38f2648d to your computer and use it in GitHub Desktop.
.reduce(:+) explanation
#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