From: Dave Thomas Date: 2008-12-09T02:55:17+09:00 Subject: Re: string to hash On Dec 8, 2008, at 11:44 AM, Jens -- wrote: > I am in the need of a method that does the following: > > get 2 arguments a string and an integer (int_val). > The string has the following form "a.b.c.d" > > the method should return a nested hash like that: > > { :a => { :b => { :c => { :d => int_val } } } } How about def nested_hash(string, val) string.split(/\./).reverse.inject(val) { |accum, val| { val.to_sym => accum } } end p nested_hash("a.b.c.d", 123)