From: Rob Biedenharn Date: 2007-06-29T09:16:10+09:00 Subject: Re: newby non/programmer trying to understand classes On Jun 28, 2007, at 8:05 PM, Igor Sutton Lopes wrote: > On Jun 28, 2007, at 8:53 PM, Rob Biedenharn wrote: > >> # create a Hash where referencing a non-existent >> # key makes its value an empty Array >> names = Hash.new { |h,k| h[k] = [] } > > Can I assume this is equivalent to perl's autovivification? Basically. If you just had: names = Hash.new then referencing non-existent keys gives nil, but they still do not exist. names['joe'] << 'hello' would give an error trying to call nil.<<('hello') By assigning to h[k], that key is created in the 'names' hash with the indicated value (an empty Array in this case). It's about the same as: names = Hash.new # or just names = {} (names['joe'] ||= []) << 'hello' -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com