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?

Rozek
  • 53
  • 10

2 Answers2

4

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

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

Hope this helps!

Zoran
  • 4,196
  • 2
  • 22
  • 33
  • 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, – Rozek Oct 26 '17 at 22:45
  • `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.). – Cary Swoveland Oct 26 '17 at 22:54
0

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

hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50