# Array select

**URL:** <https://www.ruby-forum.com/t/array-select/119951>\
**Category:** Ruby\
**Created:** [November 12, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/array-select/119951 "2007-11-12T15:06:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 12, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/array-select/119951/1 "2007-11-12T15:06:05Z")

</div>

i have 2 arrays of objects: dates and places

dates have attribute place\_id  
and places have id

I must to selezionate all places that have id in array of objects dates.

dates.select{|obj| obj.place\_id in places[].id }

How i can replace "in places[].id " to works it?  
Thanks

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 12, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/array-select/119951/2 "2007-11-12T16:09:22Z")

</div>

2007/11/12, Luca R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> i have 2 arrays of objects: dates and places
> 
> dates have attribute place\_id  
> and places have id
> 
> I must to selezionate all places that have id in array of objects dates.
> 
> dates.select{|obj| obj.place\_id in places[].id }
> 
> How i can replace "in places[].id " to works it?

Create a Set of place ids and test against that when selecting.  
Alternative use #any? on places to test for a place with the current  
date’s place\_id but this is very inefficient.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 12, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/array-select/119951/3 "2007-11-12T16:11:53Z")

</div>

Luca R. wrote:

> i have 2 arrays of objects: dates and places
> 
> dates have attribute place\_id  
> and places have id
> 
> I must to selezionate all places that have id in array of objects dates.
> 
> dates.select{|obj| obj.place\_id in places[].id }
> 
> How i can replace "in places[].id " to works it?  
> Thanks

You could use Array#collect, which creates a new array out of another  
array, taking a block of code that processes an element of the old array  
and spits out something to go into the new array.

so,  
places[].id

would be  
places.collect{ |place| place.id}

You can then ask if obj.place\_id is in this, as follows:

dates.select{ |obj| places.collect{ |place|  
place.id}.include?(obj.place\_id) }

I think that’s what you’re after (i’m not exactly sure from your  
question, sorry).

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 12, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/array-select/119951/4 "2007-11-12T16:14:11Z")

</div>

Max W. wrote:

> Luca R. wrote:
> 
> > i have 2 arrays of objects: dates and places
> > 
> > dates have attribute place\_id  
> > and places have id
> > 
> > I must to selezionate all places that have id in array of objects dates.
> > 
> > dates.select{|obj| obj.place\_id in places[].id }
> > 
> > How i can replace "in places[].id " to works it?  
> > Thanks
> 
> You could use Array#collect, which creates a new array out of another  
> array, taking a block of code that processes an element of the old array  
> and spits out something to go into the new array.
> 
> so,  
> places[].id
> 
> would be  
> places.collect{ |place| place.id}
> 
> You can then ask if obj.place\_id is in this, as follows:
> 
> dates.select{ |obj| places.collect{ |place|  
> place.id}.include?(obj.place\_id) }
> 
> I think that’s what you’re after (i’m not exactly sure from your  
> question, sorry).

Just occurred to me that this is horribly inefficient as you remake the  
collection for each member of dates! It would be better to make the new  
array first:

place\_ids = places.collect{ |place| place.id}  
dates.select{ |obj| place\_ids.include?(obj.place\_id) }
