Ruby’s Enumerable module is pretty extensive, but from time to time I wish it had some extra methods, that are available in the standard libraries of other languages like Scala, Groovy, Haskell and Clojure. I’m pretty sure I’m not the only one, that’s why I’m writing this post.
Here I’ll show you a couple of (hopefully) useful extensions I
Enumerable in the
Powerpack library. Let’s start
Pretty neat! Concise and memory efficient (compared to using
take_last were borrowed from Clojure and
I use them quite often. There are also
Summing a collection is also something that pops quite often in the wild:
1 2 3 4
While I understand why it doesn’t make sense to have this in
Enumerable by default (not every enumerable can be summed), this method is still
pretty useful and I like having it around.
Enumerable#one? is a neat (if somewhat unknown) method that lets
you quickly check if only a single element matches a predicate or a
collection has only one element that’s not logically false:
1 2 3 4
Unfortunately there is no method
Enumerable#several?, so I added one to Powerpack:
1 2 3 4
Finally, counting the frequencies of elements in a collection is a common enough task to justify having it as method:
None of these methods are spectacular, but I feel they can make the code we write a little bit more concise, efficient and readable. Hopefully some of these methods will make it one day to Ruby proper, but until then you can use them from Powerpack.
And that’s all for today!
I’d really love to hear what methods would you like to add to
Enumerable (and other core Ruby modules and classes), so please
share this with me in the comments or on
Twitter. Maybe some of them will land in Powerpack!