Last week I tackled Ruby arrays and shared some of the methods I find the most useful for working with them. This week I want to talk about HASHES! Before I dive head first into my favorite methods, I want to give a quick summary of what a hash is in Ruby and how it works. The definition of a Hash straight from the docs themselves:
A Hash is a dictionary-like collection(If you are a Java person, think maps) of unique keys and their values. Also called associative arrays(honestly, I have never heard this before but π€·), they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type.
One of the keys(pun intended π) in that definition is that a Hash allows you to use any object type as your index. Most people when they think of a Hash think of this.
{ a: 1, b: 2, c: 3 }
But hashes can be so much more in Ruby! For example, all of these are Hashes.
# String Keys
{ "a" => 1, "b" => 2, "c" => 3 }
# Integer Keys
{ 1 => "a", 2 => "b", 3 => "c" }
# Symbol Keys (Hashrocket notation)
{ :a => 1, :b => 2, :c => 3 }
# Array Keys
{ ['a'] => 1, ['b'] => 2, ['c'] => 3 }
# Hash Keys
{ { :a => 1 } => 2, { :b => 2 } => 3 }
Any object type your heart desires can be a Hash key. Now that we have the semantics and the definition down, let's get to the methods!
each
-
each_key
/each_value
-
keys
/values
-
key?
/value?
-
transform_keys
/transform_values
dig
fetch
select
reject
slice
chaining
NOTE: Just like in the array tutorial, if you see irb
in the code snippets that means I am working in a Ruby console. Also, I made a code CHEAT SHEET if you want the code without all the explanations.
each
One of the most valuable things you can do with a hash is iterate over it. One of the easiest ways to loop over a hash is to use each
. I don't know about you, but when I started out using Ruby, my hash each
statements looked like this.
irb> { a: 1, b: 2, c: 3 }.each do |pair|
puts "#{pair.first} #{pair.last}"
end
a 1
b 2
c 3
=> {:a=>1, :b=>2, :c=>3}
Here, I am executing my each
method the same way I did for my arrays. The difference now is that pair
is an array itself. The first element is my key and the second element is my value. NOTE: After executing the block for each of our key/value pairs, the original hash that we iterated over is returned.
The above way works, but there is an even slicker way, you can separate that pair by passing two arguments to your block. This means you can rewrite the above like this:
irb> { a: 1, b: 2, c: 3 }.each do |key, value|
puts "#{key} #{value}"
end
a 1
b 2
c 3
=> {:a=>1, :b=>2, :c=>3}
Now we have two separate variables, one representing the key and one representing the value.
each_key
/each_value
But what if we don't want all the keys and values? What if we just want the keys or just want the values? There are methods for each of those!
irb> { a: 1, b: 2, c: 3 }.each_key do |key|
puts key
end
a
b
c
=> {:a=>1, :b=>2, :c=>3}
irb> { a: 1, b: 2, c: 3 }.each_value do |value|
puts value
end
1
2
3
=> {:a=>1, :b=>2, :c=>3}
Notice, that once again, regardless if we use each_key
or each_value
, we still get the original hash back after we are done iterating over it.
keys
/ values
Let's say you just want an array of the keys or you just want an array of the values from a hash. To do this, you can use keys
or values
. Both will return an array of the keys or values for a given hash.
irb> { a: 1, b: 2, c: 3 }.keys
=> [:a, :b, :c]
irb> { a: 1, b: 2, c: 3 }.values
=> [1, 2, 3]
key?
/ value?
Another couple of pretty straight forward methods are the key and value predicates. If you want to know if a key or a value exists in a Hash then you can use key?
or value?
.
irb> { a: 1, b: 2 }.key?(:b)
=> true
irb> { a: 1, b: 2 }.value?(2)
=> true
irb> { a: 1, b: 2 }.value?(5)
=> false
This is pretty straightforward, but there are a couple of nuances I want to point out. For starters, when you are looking for a key, you have to make sure you are looking for the proper datatype. For example, if you have a hash with symbol keys, searching for a string will return false.
irb> { a: 1, b: 2 }.key?("b")
=> false
Make sure the datatype you are searching for matches the datatype of your keys or values.
fetch
Now we know how we can check if we have a specific key or value, but what if we want to actually look up the value for a key? Well, there is always the standard way to do it and this is what I learned first.
irb> h = { a: 1, b: 2, c: 3 }
irb> h[:a]
=> 1
irb> h[:d]
=> nil
Simple and easy. If the key is there, we return its value. If the key is not there we return nil. But what if we have some more complicated logic? For example, what if we want to return the key's value OR, if the key does not exist, we want to return some default, like 0? With what we know so far we could do this
h = { a: 1, b: 2, c: 3 }
if h[:a]
result = h[:a]
else
result = 0
end
It works, but it is a pretty decent chunk of code to get it done. Instead, we can replace that large chunk of code with one simple method, fetch
. fetch
has a lot of options and behaviors so lets unpack each, one at a time.
1) fetch
with NO arguments will return the value if it is present. If the value is not present, fetch
will raise an error. This is great if you want to raise an error when a key cannot be found.
irb> h = { a: 1, b: 2, c: 3 }
irb> h.fetch(:a)
=> 1
irb> h.fetch(:d)
KeyError: key not found: :d
from (irb):13:in `fetch'
from (irb):13
2) fetch
WITH arguments will return the value if it is present, just like before. Here is where it gets slick and can help us out with our original use case, if you pass fetch an argument and the key is not present, it will return the argument! π₯
irb> h = { a: 1, b: 2, c: 3 }
irb> h.fetch(:a, 0)
=> 1
irb> h.fetch(:d, 0)
=> 0
dig
fetch
works great when you have a single level hash and want to return a value from it using a key. But what if you have several nested hashes? For example
h = { a: { b: { c: 1 } } }
Normally to get the value of c you would want to do
irb> h[:a][:b][:c]
=> 1
which would traverse your nested hashes and return your value. But what if you are not sure whether all those levels are present? For example, let's say you are working with an API to get user data. Sometimes you find the user you are looking for and get this back.
{ user: { first: 'mary', last: 'sue' } }
Other times, you don't find the user you want and you get this back,
{ status: 'not found' }
In this case, we can't just assume we have the user information, because if we don't have the user information, we will end up raising an error.
irb> h = { status: 'not found' }
irb> h[:user][:first]
=> NoMethodError: undefined method `[]' for nil:NilClass
To avoid this, we could do
h = { status: 'not found' }
if h[:user].present?
result = h[:user][:first]
else
result = nil
end
This ensures if we have the user data, we return the first name. If we don't have the data, then we return nil. Turns out, we can avoid ALL of this by using dig
. dig
extracts the nested value specified by a sequence of key objects. However, if any one of those nested keys do not exist, it will return nil. π
irb> h = { user: { first: 'mary', last: 'sue' } }
irb> h.dig(:user, :first)
=> 'mary'
irb> h = { status: 'not found' }
irb> h.dig(:user, :first)
=> nil
dig
is great when you want to traverse a hash but are not sure what its structure might be. It allows you to work with a hash without worrying about handling a bunch of errors or doing a bunch of present?
checks using if/else blocks.
transform_keys
(Only available in Ruby 2.5 and above)
Now we are going to kick it up a notch. What if we have a hash where all the keys are symbols, but we want to change them all to strings? In the early days we would do this.
new_hash = {}
{ a: 1, b: 2, c: 3 }.each do |key, value|
new_hash[key.to_s] = value
end
# new_hash = { "a" => 1, "b" => 2, "c" => 3 }
It works, but you guessed it, now there is a better way. That better way is transform_keys
. transform_keys
allows you to iterate over a hash and will return a new hash. The new hash keys will be the result from executing the block for each of the original keys.
h = { a: 1, b: 2, c: 3 }
new_hash = h.transform_keys do |k|
k.to_s
end
# new_hash = { "a" => 1, "b" => 2, "c" => 3 }
IMPORTANT transform_keys
(above) and transform_values
(below) are only available in Ruby 2.5 and above. If you get the following error, then you are likely working with a version of Ruby below 2.5 NoMethodError: undefined method
transform_keysfor {:a=>1, :b=>2, :c=>3}:Hash
transform_values
(Only available in Ruby 2.5 and above)
transform_values
works the exact same way as transform_keys
, except it allows you to update the values. When it is done executing your block, it will return a new hash with the new set of values.
h = { a: 1, b: 2, c: 3 }
new_hash = h.transform_values do |value|
value * 2
end
# new_hash = { :a => 2, :b => 4, :c => 6 }
select
Now we are going to get into some familiar territory if you read my first array tutorial. Just like for arrays, you can use select
for hashes! select
for a hash works basically the same as an array, it returns a new hash with the key/value pairs that your block evaluated true for.
# clunky way
new_hash = {}
h = { a: 1, b: 2, c: 3, d: 4 }
h.each do |key, value|
new_hash[key] = value if value.even?
end
# new_hash = { :b => 2, :d => 4 }
# slick select way
h = { a: 1, b: 2, c: 3, d: 4 }
new_hash = h.select do |key, value|
value.even?
end
# new_hash = { :b => 2, :d => 4 }
slice
Let's say you have a hash and you just want back a specific set of keys and their values. You could do something like this
key_list = [:a, :d]
h = { a: 1, b: 2, c: 3, d: 4 }
new_hash = h.select do |key, value|
key_list.include?(key)
end
# new_hash = { :a => 1, :d => 4 }
But Ruby has a simpler way and that is by using slice
. slice
will return a hash with only the keys you request via your arguments. What we have above can be simplified to
h = { a: 1, b: 2, c: 3, d: 4 }
new_hash = h.slice(:a, :d)
# new_hash = { :a => 1, :d => 4 }
reject
Back we go again to familiar array territory. reject
for a hash works the same way it does for an array. reject
will return a new hash consisting of all the key/value pairs for which your block returns false.
h = { a: 1, b: 2, c: 3, d: 4 }
new_hash = h.reject do |key, value|
value.even?
end
# new_hash = { :a => 1, :c => 3 }
chaining
Last, but not least, we need to cover chaining. Any of the methods above that return a hash, you can chain together. For these examples I am going to use bracket notation because I think it's easier to read chaining methods from left to right, rather than up and down.
Here is an example of chaining some of the above methods together.
h = { a: 1, b: 2, c: 3, d: 4 }
result = h.transform_keys{|k| k.to_s}.slice("a", "b").reject{|k, v| v == 2}.transform_values{|value| "hi #{value}"}
# result = {"a"=>"hi 1"}
Let's break down what is happening here given what we learned above.
1) transform_keys
changes each of the hash's keys to a string and returns { "a" => 1, "b" => 2, "c" => 3 , "d" => 4}
2) slice
will select only keys "a" and "b" and their values and return { "a" => 1, "b" => 2 }
3) reject
will remove any key/value pair where the value is equal to 2, that leaves us with { "a" => 1 }
4) Our final transform_values
will change our hash's values into a string with "hi" and our number following it. The final result is {"a"=>"hi 1"}
BOOM!π₯
You made it to the end!!! Hopefully you are feeling a little more comfortable with Ruby hashes now and have a couple more methods in your toolbox that you can use. If you have any questions or anything needs clarification please don't hesitate to drop a question in the comments!
If you want a quick reference for these methods I made a handy cheat sheet with just the code from each example.
Top comments (4)
Good article overall, but β¦
This is a bit misleading.
Hash
keys must respond tohash
method.Thanks, very usefull to perform basic skills. What the difference between each and map? I guess that map has fewer methods or something like that.
Great question! The difference is in what they return.
When working with hashes
each
will run a block of code for each key/value pair in your hash and when it is done it will return the original hashmap
for a hash(just like an array) will return a new array with the result of executing your blockTL;DR
YESSSSS!!!!! π π π π