I have an array of arrays like so: [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]. I want to check whether the larger array contains arrays of a given number without regard to the animal element of the array. I also do not want to loop through the array because this would become computationally heavy.
So something like @boolean = bigArray.include?([2, *]), except something that actually works…
You have two possible solutions. Detect the element in the array or convert the array into a
Hash1. Use Enumerable#detect, it will stop as soon as it finds a match and return it,
nilotherwise.If you want to add this to the
Arrayclass like your example, and force it to return a boolean, do this:Example:
2. If you don’t care about the colliding keys, you could convert the array into a
Hashand see if the key exists.