Charles Comstock wrote:
> I just discovered a pattern that I have found to be quite useful.  In
> essence it allows for me to do automagically instantiation of variables
> for a specific type.  I have found it to be particularly useful in code
> that has multiple mutex locks.  For instance:
>
> mlock = Hash.new {|h,k| h[k] = Mutex.new }
>
> then later throughout my code when I need  mutex I say
> mlock["entry"].synchronize do
>    # blah
> end
>
> or
> mlock["exit"].synchronize do
>    # exit blah
> end
>
> It's nice because that way I don't have to keep instantiating new
> mutexes in the initalization code, I just add one whenever I need it.
> Anyway, I thought it was a useful hack and thought I would share it.
>
> Charlie


Hope I can find that if I need it.


You might want to upgrade your "keys" from pieces of text to
rock-solid symbols ... mlock[:entry]   mlock[:exit] for that
added style edge (?) :)


daz

-----

For another interesting use of the Hash.new block:
###  from:  www.ruby-talk.org/81135   (Aredridel)

hb3 = Hash.new { |h,k|
   Thread.new {
     some_task_with_immense_processing_that_assigns_to_h(h, k)
   }
   "incomplete"
}