Logan Capaldo
1/22/2007 4:33:00 AM
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| ... }