Can someone explain this line for me?


#1



If someone could explain to me why the string[1..-1] part works on the below code. I just don't seem to understand why it would be -1. Probably a really simple answer but thanks anyway...


def capitalize(string) 
  puts "#{string[0].upcase}#{string[1..-1]}"
end


#2

Try the documentation, it describes what negative indexes do
https://ruby-doc.org/core-2.2.0/String.html#method-i-slice


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.