Stupid Ruby Tricks 3: Injecting an operator
Just found out that you can inject an operator into a collection. For example:
[[1,2],[3,4]].inject(, :+) #=> [1, 2, 3, 4]
You don’t even need the starting value. Rad.
Edit: If you leave out the starting value, injecting an empty array will return nil. Include an empty array as the first parameter if you want an empty array back in that case.
Previous trick: Using find and find_all on ActiveRecord collections