From: Suraj Kurapati Date: 2009-03-31T08:15:53+09:00 Subject: Re: Is there an idiom for mass-populating Hash keys? Shot (Piotr Szotkowski) wrote: > The catch is I’ll be doing various things to this graph, > like merging vertices together, grouping them by their labels (graph > colouring), and doing similar operations on (labeled) edges. Try providing a default_proc to Hash.new: Hash.new {|h,k| h[k] = nil } Now previously unregistered vertices will be registered upon hash access (Hash#[]). To illustrate this technique, consider this "dynamic programming" based Fibonacci sequence implementation: fib = Hash.new {|h,k| k < 2 ? 1 : h[k-1] + h[k-2] } p 0 => fib[0] p 1 => fib[1] p 5 => fib[5] p :cache => fib -- Posted via http://www.ruby-forum.com/.