How can I write an argument that captures the first non-consecutive element in an array of numbers. So far I've tried using the each_cons method, which has failed empathically as you'll see in the code below. I can't figure out what to combine it with. There doesn't seem to be much documentation on how I can approach this so if any of you have any suggestions please do share.
This is what my code currently looks like:
def first_non_consecutive(arr)
arr.each_cons(6) { |a| p a } # the block is just to test the output
end
Desired outcome:
So for example if I had a sequence of [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that's the first non-consecutive number.
Current outcome:
Expected: 6, instead got: nil
Log
[1, 2, 3, 4, 5, 6] # => sequential array should return null2, would you recommend an if statement for this?
[2, 3, 4, 5, 6, 7] # => 6 is the first non-consecutive element
Please note the expression should also be able to take negative integers.