From: Roger Pack Date: 2008-09-05T00:13:52+09:00 Subject: Re: Hash optimization question > I guess it will be difficult to get both: fast hash lookup and > insertion on one hand and fast iteration on the other hand. Is it > worth the effort? I don't know. This depends on your application. > Where is this a problem for you? Maybe there is a more efficient data > structure for your particular problem. Yeah I guess it's hard to have the best of both worlds. Hmm. I suppose that for small hashes which call .each a fix would definitely help, for large hashes that call .each "a lot" it would probably also be quicker, and for any hashes which don't call .each it wouldn't be helpful. I wonder if the slowdown is small enough to make it worth it anyway [though it would use more RAM too--then again, Ruby isn't known for its low memory usage] :) In the example I was thinking of, I was using a hash to parse lines in a file a la identifiers = {/abc/ => 'an abc line', /def/ => 'a def line'} string.each_line {|l| identifiers.each{|reg, description| if l =~ reg then; do something; end } So...my particular example I'm only using a hash because of the clean syntactic look :) [not for the functionality at all]. Rails uses it quite a bit, too, hence my enquiring about it. I think that for now I'll just write a Hash monkey patcher a la a = {} a.fast_each_ify a[:abc] = :def # elements from here on are now wrapped internally to allow for a quicker .each Thanks! -=R -- Posted via http://www.ruby-forum.com/.