From: Simon Krahnke Date: 2008-12-28T12:04:45+09:00 Subject: Re: Are all Ruby built-in objects thread safe? * Robert Dober (11:08) schrieb: > On Sat, Dec 27, 2008 at 10:04 AM, Simon Krahnke wrote: >> * Robert Klemme (11:23) schrieb: >> >>> if hash.contains_key? k >>> dat = hash[k] >>> else >>> dat = create_dat(k) >>> hash[k] = dat >>> end >> >>> [...] >> >>> So in this case you need a lock around the _complete_ block of code. >>> (Note, the method level lock would work if using a Hash with a default >>> block, but this solves only a small portion of the cases.) >> >> If Hash had a method get_with_default that took a default value or a > I believe you mean > "get and assign with default" > because IIRC there is a get_with_default, it is simply called > Hash#fetch You are right. There is a little difference: fetch doesn't pass the hash to its block. But one can "abuse" both with an assignment in the block: | dat = hash.fetch(k) { | key | hash[key] = create_dat(key) } mfg, simon .... l