From: Logan Capaldo Date: 2007-01-22T13:33:13+09:00 Subject: Re: Tricky: converting path into a Hash 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| ... }