The group_by method from Ruby’s Enumerable mixin (and compared with Scala)

A few weeks ago I wrote about the groupBy method from Scala’s collection library. Today I will port those example to Ruby for which there is also such a method, just with s slightly different name (group_by instead of groupBy).

For the idea behind the method, see the post about Scala’s version. Here I will put the Scala code from the other post and show the equivalent code for Ruby next to it.
Note: I’ve used Ruby 1.9.2p290 for all examples.

1st Example:

Here we group an Array (in Scala List) of strings (with names of different bird species) by their first name.

Scala:

Ruby:

The value of the resulting Hash is shown in the comment. The line breaks were added by me to make it more readable. They are not part of the result.

2nd Example:

Here we group and Array (in Scala List) of strings by their length.

Scala:

Ruby:

3rd Example:

Here we group some raptor (birds of prey) species by their kind. Everything that is not an Eagle or Falcon will be “Unknown”.

Scala:

Ruby:

4th Example

Here we take an Array (List in Scala) of String and group them by the number of occurrences in the Array.

Scala:

Ruby:

Ruby’s Hash class does not have a mapValues method like Scala’s Maps do (at least I couldn’t find one in the standard library) , so I used inject instead which is very similar to foldLeft in Scala. This is not as elegant as mapValues but get’s the job done as well.

Conclusion

As you can see the Ruby version of the group_by method is as easy to use as Scala’s version. Both Ruby and Scala provide a lot of great tools to work with collections. Scala’s collection library is much bigger than Ruby’s standard collections, though. Scala supports many more data structures and also a lot of immutable data structures because Scala also encourages functional programming (whereas Ruby is mostly only OOP).

No matter what language you use, learn as much about the available collections in the standard library (or available open source packages) as possible because this will save you a lot of time when using those languages.

1 Comment

  1. Nice post Markus. Sorry to chime in so late but i wanted to provide a slightly shorter alternative to mapValues in the 4th example

    count = Hash[words.group_by { |w| w }.map { |k,v| [k, v.size] }]

    Hash#map returns an array (to stay consistent with Enumerable#map) so we convert it by wrapping the whole expression in Hash[…]. Still not nearly as clean as mapValues, but a slight improvement, IMO.

Leave a Reply

This blog is kept spam free by WP-SpamFree.