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?

Progman
  • 16,827
  • 6
  • 33
  • 48

0 Answers0