I have the following:
value = 42
array = ["this","is","a","test"]
how can I convert that to get this
{ "this" => { "is" => { "a" => { "test" => 42 } } } }
the array is always flat.
Thank you!
Try this:
array.reverse.inject(value) { |assigned_value, key| { key => assigned_value } }
#=> {"this"=>{"is"=>{"a"=>{"test"=>42}}}}
reverse
and inject
it first creates a Hash with the last key
("test") and the final value (42). It then takes that Hash and assigns it to the second last key
("a") and keeps going until it reaches the first key
in the original Array. The result is a perfectly nested Hash. Nice one!
May 6, 2021 at 20:19
join1:join2:join3:column1
. So I would need toeager_load(join1: {join2: :join3})