1

I want to generate nested hash from string. Input: key: a.b.c value: true Output: {:a=>{:b=>{:c=>true}}}

I have the following method from the stackoverflow: Generate nested hashes from strings and deep merging in ruby

hash = {}

"one.two.three".split('.').reduce(hash) { |h,m| h[m] = {} }

puts hash #=> {"one"=>{"two"=>{"three"=>{}}}}

This generates an empty value for the last object and I need to assign a value to it through method itself without specifying keys name.

Any idea on how to achieve this?

1
  • This question has been answered here Commented Mar 11, 2022 at 22:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.