From: Robert Klemme Date: 2009-05-05T00:03:44+09:00 Subject: Re: lazy lookup 2009/5/4 Thomas Hafner : > Robert Klemme wrote/schrieb <763388F1amqkuU1@mid.individual.net>: > >> Sorry to sound discouraging but there is memoize already. The >> difference is that not the whole Hash is filled but only the one >> that you need for the argument list provided which seems potentially >> more efficient. > > In my situation this ist not the case. The key value pairs can be > retrieved by reading a file. It's better to open/read/close the file > only once and retrieve all key value pairs at the same time. Of course > the file should not be read at all, if never a value will be > requested. > >> In case you want to fill the Hash completely you can just replace the >> calculation code to assign multiple values. > > How does that look like? Somethink like that?: > >  Hash.new{ |h,k| h.replace( >      COMPLETELY_CALCULATED_HASH >    ); h[k] } > (COMPLETELY_CALCULATED_HASH should be replaced by some code which > calculates the hash completely) I don't see the need for #replace. You can as well hand over h to the routine that fetches the data. But otherwise yes. As a safety measure you would probably also remember that you have loaded the Hash in an instance variable of h. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/