Guide to the Dig Method in Ruby
In this Ruby tutorial we are going to walk through how we can use the new Ruby Dig method.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now, the Dig method provides a really nice interface for being able to go in and to parse and to traverse hashes or hash like data. We're going to walk through a couple examples here, I'm going to start off by creating a user hash, so this is going to have a user with a name, and then it's also going to have a nested hash called favorites, and inside of favorites we'll have food, and then we'll also have movies. That is going to be exactly what we need in order to parse the data.

Now, we're going to walk through the older way of doing this, this is going to being using the bracket syntax. If I say user and then from there I say name then, I'm able to get access to that username right here and, as you can see, now it says Kristine.

large

If I wanted to use a nested hash, so if I wanted to say favorites, and then find out her favorite food, just like this, then I could run it, and you can see it returns pizza. That all works perfectly fine, but now the Dig method gives us a different interface.

I'm going to get rid of this, and I can say user, and then call the Dig method, and then pass in what I'm looking for. Here, I can say name, and now this will return Kristine.

large

Now, it also gives me a great way of grabbing the nested values, you can simply pass additional arguments to the Dig method. Here, I can say user.dig and then we'll say favorites, and then movies, just like this. Now, if I call this, this is going to return Fiddler on the Roof just like we'd expect.

large

Now, if you want to access, or if you try to access something that doesn't exist, so if I call, and I say that this is going to be something else, let me clear all of our current output, and what this is going to do is this is just going to return nil, and so that is something that is very important to understand with the Dig method is you have to be careful to make sure what kind of value you're trying to access.

For example, if I went into favorites and I did have a key here called something else, and it was set to nil by default then you're going to run into something a little bit confusing here because you're going to get nil for both cases, so you're going to get nil if the key doesn't exist, but if the value happens to be nil you'll also get nil, so that is an edge case that you really need to be careful with.

I just wanted to add that as a caveat, that is one of the only issues that you'd run into in following this approach. There are many times where you would want this to fail softly, or you don't really care if the key exists or not. You simply want to see if the value is there, if it is then you want to do something with it.

This is how you can use a hash. I'm going to just get rid of this, I'll include it in the show notes though, so you can access the code. I want to show you the second example, which is going to be showing how we can work with the YAML data structure. This is a very common approach for using the Dig method as well.

Right here, I have a config.yml file. It has a secret key, it has a production, a nested YAML production key with AWS inside of that, and then a development one. As you may have noticed, this looks very similar to the rails encrypted keys file and that's on purpose because I wanted to show you how you could use these together.

Right here we have two items that are nested, and then one that is not. I'll show you how we can actually access YAML data using Dig. I'm going to switch back to this file, and then close that out. Now, what I want to do is I first want to require the YAML library, and then from there we need to pull in the file.

I'm going to store the contents of the file in a variable called config, and I'll say YAML.load_file, and then the file is called config.yml. Now, if I output this, so if I say config.inspect then this is just going to give me the YAML file just like this, the secret key, and all of those different values.

large

Now, let's see how we can actually access these using Dig. I can say config.dig, and then from there I can't pass in a symbol because I'm working with YAML, so I need to pass it in as a string, so I can just say secret_key, just like this, and now if I run this then this is going to return password.

Now, if I want to grab a nested value, so say I wanted to grab the production AWS key then I can say production, and then aws_key, as different arguments. Now, if I run this you can see it returns 123. If I want to call development then this is going to return the 456.

large

That is how you can use dig not just for normal Ruby hashes, but you can also use them for outside data such as YAML files.

Code

user = {
  name: 'Kristine',
  favorites: {
    food: 'Pizza',
    movies: 'Fiddler on the Roof'
  }
}

user.dig(:name) # => "Kristine"
user.dig(:favorites) # => {:food=>"Pizza", :movies=>"Fiddler on the Roof"}
user.dig(:favorites, :movies) # => "Fiddler on the Roof"

require 'yaml'

config = YAML.load_file('config.yml')

config # => {"secret_key"=>"password", "production"=>{"aws_key"=>123}, "development"=>{"aws_key"=>456}}

config.dig('secret_key') # => "password"

config.dig('development', 'aws_key') # => 456