Two arrays: return only values that exist in both arrays?

Hi all

a = [:one, :two, :three, :six]
b = [:two, :four, :five: six]

I want to extract the values of two arrays which exist in both ones (a
and b):

# => [:two, :six]

Does there exist a method for this?

Thanks
Josh

···

--
Posted via http://www.ruby-forum.com/.

Try
a&b

Dima :slight_smile:

···

On Nov 23, 1:25 pm, Joshua Muheim <fo...@josh.ch> wrote:

Hi all

a = [:one, :two, :three, :six]
b = [:two, :four, :five: six]

I want to extract the values of two arrays which exist in both ones (a
and b):

# => [:two, :six]

Does there exist a method for this?

Thanks
Josh
--
Posted viahttp://www.ruby-forum.com/.

How about the elements that are in one but not the other array?

Where are these methods documented?

Thank you

Victor

···

On Nov 23, 2007 7:55 AM, Dejan Dimic <dejan.dimic@gmail.com> wrote:

On Nov 23, 1:25 pm, Joshua Muheim <fo...@josh.ch> wrote:
> Hi all
>
> a = [:one, :two, :three, :six]
> b = [:two, :four, :five: six]
>
> I want to extract the values of two arrays which exist in both ones (a
> and b):
>
> # => [:two, :six]
>
> Does there exist a method for this?
>
> Thanks
> Josh
> --
> Posted viahttp://www.ruby-forum.com/.

Try
a&b

Dima :slight_smile:

Thanks! But right - where are they documented?

···

--
Posted via http://www.ruby-forum.com/.

You're looking for "a - b", in that case.

http://ruby-doc.org/core/classes/Array.html

Hope that helps.

···

On Nov 23, 2007 8:06 AM, Victor Reyes <victor.reyes@gmail.com> wrote:

How about the elements that are in one but not the other array?

Where are these methods documented?

Thank you

Victor

On Nov 23, 2007 7:55 AM, Dejan Dimic <dejan.dimic@gmail.com> wrote:

> On Nov 23, 1:25 pm, Joshua Muheim <fo...@josh.ch> wrote:
> > Hi all
> >
> > a = [:one, :two, :three, :six]
> > b = [:two, :four, :five: six]
> >
> > I want to extract the values of two arrays which exist in both ones (a
> > and b):
> >
> > # => [:two, :six]
> >
> > Does there exist a method for this?
> >
> > Thanks
> > Josh
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
> Try
> a&b
>
> Dima :slight_smile:
>
>

Joshua Muheim wrote:

Thanks! But right - where are they documented?

in ruby-doc :slight_smile:

http://ruby-doc.org/core/classes/Array.html#M002235

--Garret

···

On Nov 23, 2007 8:21 AM, Joshua Muheim <forum@josh.ch> wrote:

Thanks! But right - where are they documented?
--
Posted via http://www.ruby-forum.com/\.

Thank you

···

On Nov 23, 2007 8:26 AM, Garret Kelly <garret.kelly@gmail.com> wrote:

You're looking for "a - b", in that case.

class Array - RDoc Documentation

Hope that helps.

On Nov 23, 2007 8:06 AM, Victor Reyes <victor.reyes@gmail.com> wrote:
> How about the elements that are in one but not the other array?
>
> Where are these methods documented?
>
> Thank you
>
> Victor
>
>
> On Nov 23, 2007 7:55 AM, Dejan Dimic <dejan.dimic@gmail.com> wrote:
>
> > On Nov 23, 1:25 pm, Joshua Muheim <fo...@josh.ch> wrote:
> > > Hi all
> > >
> > > a = [:one, :two, :three, :six]
> > > b = [:two, :four, :five: six]
> > >
> > > I want to extract the values of two arrays which exist in both ones
(a
> > > and b):
> > >
> > > # => [:two, :six]
> > >
> > > Does there exist a method for this?
> > >
> > > Thanks
> > > Josh
> > > --
> > > Posted viahttp://www.ruby-forum.com/.
> >
> > Try
> > a&b
> >
> > Dima :slight_smile:
> >
> >
>

Programming Ruby
The Pragmatic Programmer's Guide

class Array
instance methods
& arr & anOtherArray -> anArray

Set Intersection---Returns a new array containing elements common to
the two arrays, with no duplicates.

[ 1, 1, 3, 5 ] & [ 1, 2, 3 ] >> [1, 3]

···

On Nov 23, 3:13 pm, Victor Reyes <victor.re...@gmail.com> wrote:

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Thank you

On Nov 23, 2007 8:26 AM, Garret Kelly <garret.ke...@gmail.com> wrote:

> You're looking for "a - b", in that case.

>class Array - RDoc Documentation

> Hope that helps.

> On Nov 23, 2007 8:06 AM, Victor Reyes <victor.re...@gmail.com> wrote:
> > How about the elements that are in one but not the other array?

> > Where are these methods documented?

> > Thank you

> > Victor

> > On Nov 23, 2007 7:55 AM, Dejan Dimic <dejan.di...@gmail.com> wrote:

> > > On Nov 23, 1:25 pm, Joshua Muheim <fo...@josh.ch> wrote:
> > > > Hi all

> > > > a = [:one, :two, :three, :six]
> > > > b = [:two, :four, :five: six]

> > > > I want to extract the values of two arrays which exist in both ones
> (a
> > > > and b):

> > > > # => [:two, :six]

> > > > Does there exist a method for this?

> > > > Thanks
> > > > Josh
> > > > --
> > > > Posted viahttp://www.ruby-forum.com/.

> > > Try
> > > a&b

> > > Dima :slight_smile: