From: Trans Date: 2005-04-13T23:44:39+09:00 Subject: Re: help traversing and modifying hash key and value inplace Robert Klemme wrote: > There's a subtle thing: you decide whether you traverse on the other > instance *after* the block got it. Dunno whether that is what you want but > I think I remember the other version decided this based on the original > value. Good catch. Thank you, so: def traverse(&b) inject({}) do |h,(k,v)| nk, nv = b[k,v] h[nk] = (Hash === v ? v.traverse(&b) : nv) h end end > You probably want to leave that out altogether and have the block > decide this. In this case traverse would become even simpler. Hmmm... not sure what "this" refers to. Assuming you mean, whether to traverse or not. Something like: def traverse(&b) inject({}) do |h,(k,v)| nk, nv = b[k,v] h[nk] = nv h end end Is that right? In that case I wouldn't call it "traverse" though --more like a hash verion of collect. Actually I think I already have that in the libs as Enumerable#build_hash. > Ah, and traverse will crash for recursive structures. Should I go through the complexity of preventing that? Well, at very least I will note it in the docs. Thanks robert, T.