I created a simple method that sorts characters in a string and returns true if "b" is within three (or fewer) characters after an "a" and vice-versa.
Here it is below:
def near_ab(string)
arr = string.split("")
a_position = arr.each_with_index.select {|i| arr[i] == "a"}
b_position = arr.each_with_index.select {|i| arr[i] == "b"}
if a_position - b_position <= 3 #arr[a] - arr[b] == 3
return true
else
return false
end
end
However, after running it I get the following error:
`[]': no implicit conversion of Array into Integer (TypeError)
Why is it giving me this error and how should I approach resolving it?