11 Tips for Using Ruby Hashes

July 27, 2016 | Ruby, Web Development

Ruby hash

A hash is one of the most frequently used data types in Ruby and several other programming languages. A hash is a collection of key-value pairs, like this: “car” => “red”. It is similar to an array, except indexing is done by keys in the hash, not an integer index. I’ve compiled this list as some of my favorite tips for using Ruby hashes.

You can find some of information over at the Ruby Style Guide on Github as well as the links I’ve included throughout the post.

1. When creating a hash, prefer the literal notation instead of the constructor notation, unless you need to pass a parameter(s).

2. Prefer the use of
size versus countFor Enumerable objects other than Array, Ruby will iterate the entire collection in order to determine its size, which results in increased processing time.

3. Prefer symbols instead of strings as hash keys.
This is important because using symbols not only saves time when doing comparisons, but also saves memory, because they are only stored once (Full answer on Stack Overlflow).

4. Avoid the use of mutable objects as hash keys.
Mutable objects are ones that can be changed in place (such as an Array), while immutable objects require the creation of a new object if you want to update the value (such as an Integer). Short answer: When we add a key-value pair to a hash, a key’s hash is also stored with the pair. When searching for a key, Ruby first compares the key’s hashes and then the keys. Using a mutable item as a key means the key’s hash will change when the key changes. This leads to some interesting and unintended effects, as documented in this blog post (long answer).

5. Use the hash literal syntax when your hash keys are symbols.
Related: don’t mix literal syntax and rocket syntax. If you’re already using literal syntax on a project, stick with it for consistency.

6. Use
Hash#key? instead of Hash#has_key? and Hash#value? instead of Hash#has_value?. As noted here by Matz, the longer forms are considered deprecated.

7. Use Hash#each_key instead of Hash#keys.each and Hash#each_value instead of Hash#values.each (syntatic sugar).

8. Use the 
||= (Double Pipe / Equals) operator for key retrieval or assignment. This is helpful when you’re iterating over a hash and you need to retrieve a value from a key if it exists or create a new one if it doesn’t. Basically, x ||= y returns x if x is true, or assigns it y if false or nil. The Double Pipe / Equals operator can be kind of tricky and has some misconceptions so I recommend reading up on it here.

9. The splat operator ( * ) can be used to quickly convert Enumerables (such as hashes).

10. You can quickly create a hash from a list of values by using

Using mutable objects (like arrays) as default values can lead to problems such as mutating the default value of the hash.

Comments or suggestions for this list? Leave them in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *