You can try to do this with this code:
keys = "one.two.three".split '.' # => ["one", "two", "three"] params = {}; value = 1; i = 0; # i is an index of processed keys array element keys.reduce(params) { |hash, key| hash[key] = if (i += 1) == keys.length value # assign value to the last key in keys array else hash[key] || {} # initialize hash if it is not initialized yet (won't loose already initialized hashes) end } puts params # {"one"=>{"two"=>{"three"=>1}}}
source share