How to Access elements of nested hashes in Ruby? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report RIn this article, we will discuss how to access elements of nested hashes in Ruby. We can access elements of nested hashes through different methods ranging from using the dig method to the fetch method. Table of Content Access elements of nested hashes using square bracketsAccess elements of nested hashes using dig methodAccess elements of nested hashes using fetch methodAccess elements of nested hashes using square bracketsIn this method, square brackets are used with keys to access nested hash elements. Syntax: nested_hash[key1][key2]... Example: In this example, we use square brackets with keys to access nested hash elements Ruby # Example nested hash nested_hash = { key1: { key2: { key3: "geeksforgeeks " } } } # Access elements of nested hashes using square brackets value1 = nested_hash[:key1][:key2][:key3] puts "Value: #{value1}" OutputValue: geeksforgeeks Access elements of nested hashes using dig methodThe dig method in Ruby's Hash class allows us to access nested hash elements using a sequence of keys as arguments Syntax: nested_hash.dig(key1, key2, ...) Example: In this example, we use dig method to access the value by passing the keys as arguments Ruby # Example nested hash nested_hash = { key1: { key2: { key3: "geekforgeeks" } } } # Access elements of nested hashes using dig method value2 = nested_hash.dig(:key1, :key2, :key3) puts "Value: #{value2}" OutputValue: geekforgeeks Access elements of nested hashes using fetch methodThe fetch method allows us to access nested hash elements. Syntax: nested_hash.fetch(key1).fetch(key2)... Example: In this example we use fetch method to access the nested hash elements Ruby # Example nested hash nested_hash = { key1: { key2: { key3: "geekforgeeks" } } } # Access elements of nested hashes using fetch method value3 = nested_hash.fetch(:key1).fetch(:key2).fetch(:key3) puts "Value: #{value3}" OutputValue: geekforgeeks Comment More infoAdvertise with us Next Article How to Convert Hash to JSON in Ruby? A abhay94517 Follow Improve Article Tags : Ruby Similar Reads How to access array Elements in Ruby In this article, we will learn how to access array elements in Ruby. In Ruby, there are several ways to retrieve the elements from the array. Ruby arrays provide a lot of different methods to access the array element. But the most used way is to use the index of an array. Using Index - ruby # Ruby p 2 min read How can we access the entries of a Hash in Ruby? In this article, we will discuss how to access the entries of a Hash in ruby. We can access the entries of a Hash through different methods ranging from using keys, and values to each method Table of Content Accessing the entries of a Hash using Hash.keys methodAccessing the entries of a Hash using 2 min read How to Make a Custom Array of Hashes in Ruby? Prerequisites: Hashes and Arrays in Ruby Arrays and hashes are data structures that allow you to store multiple values at once. In this article, we will explore their syntax, how to combine functionalities of both to create an array of hashes, retrieve values, and loop through them. An array is a co 3 min read How to iterate over a Hash in Ruby? In this article, we will discuss how to iterate over a hash in Ruby. By Iterating over a hash we can access key-value pairs and perform operations on them. Let us study different methods to iterate over a hash in ruby Table of Content Iterating over a hash using each methodIterating over a hash usin 2 min read How to Convert Hash to JSON in Ruby? Ruby Hash is an unordered set of data in key-value pairs. These are mutable and can store multiple data types. JSON stands for Javascript Object Notation and is a human-readable file format that is commonly used in web services and API calls. In this article, we will discuss how to convert a hash to 2 min read How to Convert JSON to Hash in Ruby? In this article, we will discuss how to convert JSON to hash in Ruby. We can convert JSON to hash through different methods ranging from parsing JSON and JSON.load to using YAML.safe_load Method. Table of Content Using JSON.parse MethodUsing JSON.load MethodUsing YAML.safe_load Method Using JSON.par 2 min read Like