Say I have an arbitrarily deep nested hash h:
h = {
:foo => { :bar => 1 },
:baz => 10,
:quux => { :swozz => {:muux => 1000}, :grimel => 200 }
}
And let me have a class Cthat is defined as:
class C
attr_accessor :dict
end
How to replace all nested values in h, so that now they are instances Cwith the attribute dictset for this value? For example, in the above example, I expect to have something like:
h = {
:foo => <C @dict={:bar => 1}>,
:baz => 10,
:quux => <C @dict={:swozz => <C @dict={:muux => 1000}>, :grimel => 200}>
}
where <C @dict = ...>represents instance Cc @dict = .... (Note that once you reach a value that is not nested, you will stop wrapping it in instances C.)
source
share