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

J

Joshua Muheim

Hi all

a = [:eek:ne, :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
 
D

Dejan Dimic

Hi all

a = [:eek:ne, :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

Try
a&b

Dima :)
 
V

Victor Reyes

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

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

Where are these methods documented?

Thank you

Victor

Hi all

a = [:eek:ne, :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

Try
a&b

Dima :)
 
G

Garret Kelly

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

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

Hope that helps.

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

Where are these methods documented?

Thank you

Victor


Hi all

a = [:eek:ne, :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

Try
a&b

Dima :)
 
V

Victor Reyes

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

Thank you

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

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

Hope that helps.

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

Where are these methods documented?

Thank you

Victor


Hi all

a = [:eek:ne, :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 :)
 
D

Dejan Dimic

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

Thank you

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

Hope that helps.
How about the elements that are in one but not the other array?
Where are these methods documented?
Thank you
Victor
Hi all
a = [:eek:ne, :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 :)


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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top