ActiveSupport: Array Extensions
It seems like such a simple thing, but I’m really enjoying reading the source of Rails. I’m finding all sorts of hidden gems (ouch).
For instance, check out this interesting extension added to Array in “core_ext/array/extract_options.rb”
I think I’ve rolled my own version of this at least a few times after seeing too many arguments in the signature.
Or consider this clever extension in “core_ext/array/grouping.rb”
Having just gone through “Learn You a Haskell for Great Good”, I’ve been wanting to see some Ruby examples in the wild of something which should be familiar to the functional and/or lispy crowds. Since Enumerable#inject is not used as much as it should be, let’s review it’s usage.
Inject (also called reduce) takes an enumerable (a thing which meets a couple requirements but basically means you can iterate over) and applies an operation to all of it’s elements and accumulates a resulting value. Simple case:
You can also leave off the accumulator if the first element of your enum is an acceptable starting accumulator, so the above could be
In addition, it can take a block instead of a binary operation:
Now that we’ve reviewed that, we can see the cleverness in Array#split. It has a starting accumulator of
[] and instead of a simple binary operation
:+ it takes a block and applies it to each element in enum. That’s where the logic happens, if the block returns a truthy value, we start a new empty array in the accumulator, otherwise we push the element onto the last array in the accumulator.
That’s it for now!