Deep search for a value in a hash

35 views Asked by At

I would like to analyze several hashes to extract certain specific information.

These hashes have a global tree structure, however each of the branches of this tree structure is not always present in each of the hashes. (see example for more details)

The problem is that when I use the .present? method or .presence on the data of a hash (which can sometimes not be present) I sometimes get the error undefined method []' for nil:NilClass` (in the case where the data sought is not present). I could add .present? and/or .presence everywhere, but I would like something more dynamic.

I use the has_key?() method to check if the key I'm looking for is present in my hash, but this only analyzes the first keys.

However, the hash I want to check sometimes contains multiple hashes nested in other hashes nested in arrays and so on.

How do I check the presence of my key in my hash globally and then extract the value? How can I test each stratum of a "hash branch" to be sure that it is present and avoid getting this error?

I hope I was clear enough. If this is not the case, do not hesitate to ask me any questions.


Example of hash structure:

my_hash =

{
    "key1": "value1",
    "key2": {
        "key3": "value3",
        "key4": [
            {
                "key5": "value5"
            },
            {
                "key6": "value6"
            }
        ]
    },
    "key7": [
        {
            "key8": "value8",
            "key9": {
                "key10": "value10",
                "key11": [
                    "value11"
                ],
                "key12": [
                    "value12"
                ]
            }
        },
        {
            "key13": "value13",
            "key14": [
                {
                    "key15": "value15",
                    "key16": "value16"
                },
                {
                    "key17": "value17",
                    "key18": {
                        "start": "2022-02-03",
                        "end": "2025-02-03"
                    }
                },
                {
                    "key19": "value19",
                    "key20": {
                        "key21": [
                            {
                                "key22": "value22",
                                "key23": "value23"
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

I think a recursive method could do the trick, but I haven't been able to find the right one. I would like the function (called, with parameters) to look something like this:

TestHelper.deep_find_value_w_key(my_hash, hash_w_the_key_is_searched, key_searched)

For example : I want the value of "key11", which is always in the hash nammed by "key9". So my called fonction would look like this : TestHelper.deep_find_value_w_key(my_hash, "key9", "key11") Result : "value11" And if it doesn't exist => nil

0

There are 0 answers