Q:

How do I find the first string of differing case in an array?

I have an array of strings, and all contain at least one letter:
["abc", "FFF", "EEE"]

How do I find the index of the first string that is of a different case than any previous string in the array? The function should give 1 for the above since:
FFF".eql?("FFF".upcase)

and that condition isn't true for any previous string in the array, whereas:
["P", "P2F", "ccc", "DDD"]

should yield 2 since "ccc" is not capitalized and all its predecessors are.
I know how to find the first string that is capitalized using
string_tokens.find_index { |w| w == w.upcase }

but I can't figure out how to adjust the above to account for differing case.

A:

You could take each consecutive pair of elements and compare their upcase-ness. When they differ, you return the index.
def detect_case_change(ary)
  ary.each_cons(2).with_index(1) do |(a, b), idx|
    return idx if (a == a.upcase) != (b == b.upcase)
  end
  nil
end

detect_case_change ["abc", "FFF", "EEE"] # => 1
detect_case_change ["P", "P2F", "ccc", "DDD"] # => 2

