I am trying to .insert a space before the Uppercase letter if it's found. Here's what I came up with, but it seems like it's an infinite loop. I don't know why:
def solution(string)
str = string.split("")
str.each_with_index do |l, i|
if l.upcase
str.insert(l[i], " ")
end
end
str.join("")
end
please let me know what I'm missing.
if l.upcasemeans "if a character can be successfully converted to uppercase", this is not the same as "if a character is upper case". You're effectively trying to add a space in front of the first character that can be converted to uppercase, this shifts the character's position forward. With the next iteration of the loop you re-test the same character and add another space, and so on…if l.upcasewithif ('A'..'Z').include?(l). you should experience an infinite loop anyway though