Ok so this is a follow on post for Ruby Hash - Fetch - Returning a (Default) Value for a Key That Does Not Exist which I wrote yesterday.
Basically I found that you can use the fetch method on a hash to either retrieve a value for a key or return a specified value. In the posts comments someone mentioned that it was useless for nested hashes, which is true. It turns out that it is not that hard to implement the behaviour of fetch for fetching a nested key / value pair.
For this I have added a few methods to the Hash class (if your using rails you could make this an initializer). The deep_fetch method will behave exactly the same as fetch but search the hash at all levels for a keys value.
1 2 3 4 5 6 7 8 9 10 | |
Here is an example of a nested hash we are going to work with.
1
| |
It will find a keys value at the first level of the hash (this is the same behaviour as fetch).
1
| |
If no default value is passed as the second argument a KeyError will be raised if the key is not found (this is the same behaviour as fetch).
1
| |
It will return a default value for a key that does not exist if the default argument is passed to the method.
1
| |
It can accept a block for calculated default values.
1
| |
It can fetch a value for a deeply nested key.
1 2 | |