You have two possible solutions. Detect the element in the array or convert the array into a Hash
1. Use Enumerable#detect, it will stop as soon as it finds a match and return it, nil otherwise.
> a = [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]
=> [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]
> a.detect{ |(n, _)| n == 2 }
=> [2, "cat"]
> a.detect{ |(n, _)| n == 10 }
=> nil
If you want to add this to the Array class like your example, and force it to return a boolean, do this:
class Array
def custom_include?(num)
!!detect{ |(n, _)| num == n }
end
end
Example:
> a.custom_include?(2)
=> true
> a.custom_include?(10)
=> false
2. If you don't care about the colliding keys, you could convert the array into a Hash and see if the key exists.
> a = [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]
=> [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]
> Hash[a][2]
=> "bird"
> Hash[a][10]
=> nil
bigArray.include...return[2, "cat"], or should it return["cat", "bird"], or what?