Anishluke92 / RubySolution

Problem solving using Ruby programming language
0 stars 1 forks source link

Flatten a hash #68

Open danielpaul opened 3 years ago

danielpaul commented 3 years ago

This problem was asked by Stripe.

Write a function to flatten a nested dictionary. Namespace the keys with a period.

For example, given the following dictionary:

{
    "key": 3,
    "foo": {
        "a": 5,
        "bar": {
            "baz": 8
        }
    }
}

it should become:

{
    "key": 3,
    "foo.a": 5,
    "foo.bar.baz": 8
}

You can assume keys do not contain dots in them, i.e. no clobbering will occur.