Sometimes we’d like to build a new collection object from the elements of another collection. One trivial example would be element occurrence counting, which basically means you need to build a hash from an array.
People coming from an imperative background will probably implement this in terms of
1 2 3 4 5 6 7 8 9 10
This is a reasonable solution, but surely we can do better!
Rubyists fond of functional programming techniques might use
reduce to solve the problem at hand:
1 2 3
This code works well, but it’s a bit more complex than it needs to be –
mostly because of the need to return the hash explicitly in
1 2 3
each_with_object invokes its block for each element with an
arbitrary object argument, and returns the initially given object,
thus eliminating the need to return it ourselves as the block’s
result. Simple and neat!
That’s all for now, folks. I hope you’ll find this article useful. As usual I’m looking forward to hearing your thoughts here and on Twitter!