# Splitting Array - DRY

**URL:** <https://www.ruby-forum.com/t/splitting-array-dry/112452>\
**Category:** Ruby\
**Created:** [August 19, 2007, 8:56am UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452 "2007-08-19T08:56:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [August 19, 2007, 8:56am UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452/1 "2007-08-19T08:56:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [August 19, 2007, 9:58am UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452/2 "2007-08-19T09:58:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [August 20, 2007, 8:57am UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452/3 "2007-08-20T08:57:51Z")

</div>

On 2007-08-19 09:58:46 +0200, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > 
> 
> ```
> 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

thanks Stefan, I realize that, too frequently, I am programming Ruby as  
C … not using the OO potential  
what’s one of the best book about “Programming Ruby, the way it should  
be…” ?  
I need to practise outside my app dev

note : also found that the ‘week’ has been already written… :  
(ad.valid\_until - ad.created\_at) \>= 2.weeks

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [August 20, 2007, 9:59am UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452/4 "2007-08-20T09:59:57Z")

</div>

> what’s one of the best book about “Programming Ruby, the way it should  
> be…” ?

the Ruby Cookbook (ISBN: 0-596-52369-6) is really great, as it’s  
almost entirely working examples with lots of commentary (I learn by  
doing/copying/changing), and has a curve that makes it great for  
learning and also for later reference. also many of the examples  
showcase various gems, which is very helpful for me. The table of  
contents makes it very easy for me to find exactly what I’m looking  
for. (Chapter 4: Arrays… 4.13: Extracting Portions of Arrays) highly  
recommended!

being a beginner to programming in general, Beginning Ruby (ISBN:  
1590597664) covers a lot of great information, including a whole big  
chapter on documentation, (I wouldn’t have thought of looking into  
RDoc otherwise!) error handling, debugging and testing… seeing as  
how unit testing seems like a big part of ruby, and how I just  
wouldn’t have thought of looking into it, I find this very helpful as  
well. and that’s where I am in that book, about halfway, chapter  
8/16+appendices, so it’ll probably get somewhat advanced. knowing C  
already, I don’t know if I could recommend it, but perhaps some  
foundation-up learning could be helpful. The early stuff shouldn’t be  
TOO painful, as I’d bet that ruby and C differ pretty early on.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [August 20, 2007, 9:56am UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452/5 "2007-08-20T09:56:57Z")

</div>

2007/8/20, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > ad.created\_at) / 86400.0 \> 7) && ((ad.valid\_until - ad.created\_at) /  
> > > class ClassOfAd  
> > > Regards  
> > > Stefan
> 
> thanks Stefan, I realize that, too frequently, I am programming Ruby as  
> C … not using the OO potential  
> what’s one of the best book about “Programming Ruby, the way it should  
> be…” ?  
> I need to practise outside my app dev
> 
> note : also found that the ‘week’ has been already written… :  
> (ad.valid\_until - ad.created\_at) \>= 2.weeks

Basically you need a more generalized version of partition. Maybe  
something like this works:

@ads\_id = advertisings.inject(Hash.new {|h,k| h[k]=[]}) do |h,ad|  
h[(((ad.valid\_until - ad.created\_at) / 86400 - 1) / 7] \<\< ad.id  
h  
end

Note, I’m using interger arithmetic to reduce the number of result  
values.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [August 22, 2007, 8:55am UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452/6 "2007-08-22T08:55:45Z")

</div>

On 2007-08-20 09:56:02 +0200, “Robert K.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > > > @ads\_id\_w2 = advertisings.map {|ad| ad.id if (((ad.valid\_until -
> > 
> > (ad.valid\_until - ad.created\_at) \>= 2.weeks
> 
> Kind regards
> 
> robert

Thanks Robert… I’ll put it in my Personal Ruby Cookbook, that’s  
DRYissimo !

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [August 22, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/splitting-array-dry/112452/7 "2007-08-22T14:47:14Z")

</div>

On Aug 20, 3:58 am, “Simon S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > what’s one of the best book about “Programming Ruby, the way it should  
> > be…” ?
> 
> the Ruby Cookbook (ISBN: 0-596-52369-6) is really great, as it’s  
> almost entirely working examples with lots of commentary (I learn by  
> doing/copying/changing), and has a curve that makes it great for  
> learning and also for later reference.

I have this book too, and I haven’t really delved into it, but I can  
tell it’s definitely worth having. After all, it’s an O’Reilly book.

> TOO painful, as I’d bet that ruby and C differ pretty early on.  
> The Pickaxe (the official title is “Programming Ruby - the Pragmatic  
> Programmers’ Guide, 2nd Ed.”) is also an excellent resource - highly  
> informative, well formated code examples, very readable. Might not get  
> shelved under comedy, but still pretty entertaining, for a programming  
> book.

If you’re LOOKING for comedy, \_why’s (Poignant) Guide to Ruby is where  
it’s at. [poignantguide.net](http://poignantguide.net) 'Nuff said.

GL, HF
