9

Have arrays [1, 2, 5] and [1, 2, 3] I would like to extract matching values, if there is a method like:

[1, 2, 5].match([1, 2, 3]) #=> [1, 2]

Is there any method on array, thank you

1

1 Answer 1

28

Very simple:

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

Other useful array operations include:

[1,2,3] | [1,3,4]  #=> [1,2,3,4]
[1,2,3] - [1,3,4]  #=> [2]
[1,2,3] + [1,3,4]  #=> [1,2,3,1,3,4]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.