The Ruby Rogues book club is currently reading Eloquent Ruby and I thought I’d tag along and read the book, too. I’ll be posting my thoughts on the book in this post and in a few future posts.
Chapter 3: Take Advantage of Ruby’s Smart Collections
This chapter details the use of the Hash and Array classes and that they can be used instead of specialized custom classes in most cases. That’s not really surprising and also that’s not what I’m taking away from this chapter. The good point are the last two pages where he mentions that there are cases where you shouldn’t use Array or Hash but rather use specialized collections when they make sense.
His example is the following: Imagine that we want to know if a word appears in a document. We could either use a Hash for this
or an Array
However both approaches aren’t ideal because in the case of the Hash we aren’t interested in the values we store and in the case of the Array we need to make sure the Array contains no duplicates. It turns out that in this case we should use the Ruby Set class because it’s just made for this:
1 2 3
Other collections from the standard library
This got me thinking and I went through the standard library to look for other collections that you make overlook:
Many times we use Hashes to store data like this:
If we need to pass that data around a lot it might be cleaner to create a dedicated class for it. However if it’s just a container for the data and doesn’t have any other functionality we could also use a struct. This way we get a more expressive name:
Matrix and Vector
Matrices and Vectors could be represented by Arrays but there’s really no point in not using the Matrix and Vector classes provided by the matrix standard library.
A quick look through the standard library didn’t reveal any other collections. At least I didn’t see any, but I’d be happy to be proven wrong.