From: Robert Klemme Date: 2008-02-23T01:15:06+09:00 Subject: Re: ||= [] idiom 2008/2/22, Leslie Viljoen : > On Fri, Feb 22, 2008 at 2:33 PM, Thufir wrote: > > On Wed, 20 Feb 2008 07:21:15 +0900, MenTaLguY wrote: > > > > > I sometimes do this sort of thing: > > > > > > ( @categories[resource.tag] ||= [] ) << resource > > > > > > Although this might possibly be more readable: > > > > > > category = ( @categories[resource.tag] ||= [] ) category << resource > > > > > > Often it can make sense to factor the ||= portion into a separate > > > method: > > > > > > def category_for(tag) > > > @categories[tag] ||= [] > > > end > > > > > > # ... > > > > > > category_for(resource.tag) << resource > > > > > > -mental > > > > What's the real-world application of this? > > It's for making hashes of arrays: I may be wrong here but I read Thufir's question differently: it seems he is asking for the application of refactoring this into a separate method. Kind regards robert -- use.inject do |as, often| as.you_can - without end