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?
2 Answers
Ruby's #include? method can be used here:
arr = [[1, 2], [3, 4], [4, 5]]
arr.include?([1, 2])
# => true
Hope this helps!
2 Comments
Rozek
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,
Cary Swoveland
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.).Just use bigarray.include? smallarray. It's the same way you'd check for membership with any other list. Example