From: Jos Backus Date: 2008-02-20T07:28:23+09:00 Subject: Re: ||= [] idiom On Wed, Feb 20, 2008 at 07:11:31AM +0900, Leslie Viljoen wrote: > I often make use of this idiom to add something to an array in a hash of arrays: > > @categories = {} > > pagelist.each do |resource| > @categories[resource.tag] ||= [] > @categories[resource.tag] << resource > end @categories = {} pagelist.each do |resource| (@categories[resource.tag] ||= []) << resource end or @categories = Hash.new {|h, k| h[k] = []} pagelist.each do |resource| @categories[resource.tag] << resource end -- Jos Backus jos at catnook.com