TIL: How to Prettify Ruby's inject Method
Using each_with_object over inject
So many times using Enumerable#inject
I've forgotten to return the value object.
(1..10).inject({}) { |hash, x| hash[x] = true; hash }
Which is also kinda ugly. Instead, use each_with_object
which doesn't care about the return type.
(1..10).each_with_object({}) { |x, hash| hash[x] = true }
Note that the block params are switch around though.
Member discussion