On Thu, Feb 4, 2010 at 7:33 PM, Mr Bubb <jcabraham / gmail.com> wrote:
> Well, in my job (bioinformatics), I only use code like this about a
> hundred times a day. I've been doing this job for 15 years, and
> actually, there is no better way. You must not need to quickly create
> data structures from flat files, I guess.

Let me know if my detailed explanation didn't make sense.  The long
and the short of it is this:

auto_nesting_hash = Hash.new {|hash,key| hash[key] =
Hash.new(&hash.default_proc)}

It may well be the best way to do whatever it is you are doing.  What,
I think, Marnen was trying to communicate, though, is that there may
be an alternative that performs better in Ruby which would become more
obvious if we understood the actual use a little better.

Just as a quick example, there was a thread last week where someone
was asking about while loop performance, and wondering why a while
loop in Ruby isn't so fast as he expected.

i = 0
while i < 10000000
  i += 1
end

Perfectly legitimate Ruby, and comparable to similar looping
structures in other languages.  But, the real answer there is that
there's another way to do it in Ruby that's substantially faster.

0.upto(10000000) {|i| #do whatever you need to do with i }

So, if the auto nesting hashes work for you, that's great.  If you
want to bend people's minds towards perhaps suggesting a better
alternative, though, provide more details. Someone may surprise you
with something unexpected, and superior.


Kirk Haines