I came across this confusion while practicing index in Introductory Ruby
str = “abcd”
p str[0] # “a”
p str[4] # nil
p str[4…-1] # “”
p str[5…-1] # nil
puts “123” + str[4…-1] # this worked “123” + “”
I know that str[4] gives nil since the max index is 3 for my string. I know that nil cannot be added to a string.
But str[4…-1] gives the value of “” instead of nil, while str[5…-1] still gives nil.
I would appreciate any explanation to my confusion here.