From: Stefan Rusterholz Date: 2007-08-19T16:58:46+09:00 Subject: Re: splitting Array - DRY Josselin wrote: > I am presently splitting an array on 4 successives periods of time , is > there a DRYest way to do it, or that's the way to go ? thanks > > @ads_id_w1 = advertisings.map {|ad| ad.id if ((ad.valid_until - > ad.created_at) / 86400.0 <= 7)}.compact > > @ads_id_w2 = advertisings.map {|ad| ad.id if (((ad.valid_until - > ad.created_at) / 86400.0 > 7) && ((ad.valid_until - ad.created_at) / > 86400.0 <= 14)) }.compact > > @ads_id_w3 = advertisings.map {|ad| ad.id if (((ad.valid_until - > ad.created_at) / 86400.0 > 14) && ((ad.valid_until - ad.created_at) / > 86400.0 <= 21)) }.compact > > @ads_id_w4 = advertisings.map {|ad| ad.id if (((ad.valid_until - > ad.created_at) / 86400.0 > 21) && ((ad.valid_until - ad.created_at) / > 86400.0 <= 31)) }.compact class ClassOfAd def week # may want to name it differently return nil unless whateverdate.between?(acceptable_start, acceptable_end) # your call here ((ad.valid_until - ad.created_at).div(604800)) % 4 end @ads_id = Array.new(4) { [] } advertisings.each {|ad| @ads_id[ad.week] = ad.id if ad.week } Depending on your needs that code can be condensed a bit more. Regards Stefan -- Posted via http://www.ruby-forum.com/.