1

I have an array of arrays, f.e: [[1,2],[3,4],[4,5]]. I want to check if this collection contains array [1,2]. Is there any way to do so without hardcoding it?

0

2 Answers 2

4

Ruby's #include? method can be used here:

arr = [[1, 2], [3, 4], [4, 5]]
arr.include?([1, 2])
# => true

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that solved my problem. For a long time I thought I had a problem with an array flattening, but apparently that was a bug in my own code,
include? is more direct, but you could instead use arr.index([1,2]) (returns nil or a truthy value) or (arr & [1,2]).any? or arr - [1,2] != arr (etc.).
0

Just use bigarray.include? smallarray. It's the same way you'd check for membership with any other list. Example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.