I’m looking at some external code and saw a line of Ruby code that looks like this
string_name = string_name[3..-1]
what does the [n..-x] do or mean?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ruby supports negative indexing of arrays. So index -1 is the last element of the array, -2 is the second to last, etc. Think of starting at the beginning of the array, and wrapping around from the back.
So in this case,
string_names[3..-1]is basically a substring from 3 to the end of the string.