From: Robert MannI Date: 2007-01-22T19:32:22+09:00 Subject: Re: Tricky: converting path into a Hash ------=_Part_144107_23222959.1169461939916 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Gracias everyone! I actually thought about the inject trick in bed at dawn but Ken Bloom's solution is better for me, because it doesn't override any data once I add paths to the tree. Grateful for all the smart answers. - Rob the rabbit On 1/22/07, Logan Capaldo wrote: > > On Mon, Jan 22, 2007 at 01:10:08PM +0900, Ken Bloom wrote: > > > > Then the inject trick becomes > > > > pr = lambda {|h,k| h[k] = Hash.new(&pr)} > > z=Hash.new(&pr) > > s = "a/b/c" > > s.split("/").inject(z){|ha,co| ha[co]} > > s = "a/b/d" > > s.split("/").inject(z){|ha,co| ha[co]} > > s = "a/d/e" > > s.split("/").inject(z){|ha,co| ha[co]} > > s = "c/a/t" > > s.split("/").inject(z){|ha,co| ha[co]} > > p z > > > > For some reason you can't name the hash h, otherwise the parameter > > assignment in the proc will override it. Why is that, and is there any > way > > to avoid it? > Same reason as: > > h = 3 > lambda { h = "three" }.call > p h # prints "three" > > Block arguments act like assignment. This can be seen in pathological > examples like lambda { |$a_global| ... } and lambda { |@an_ivar| ... } > > > > ------=_Part_144107_23222959.1169461939916--