string = "Yellow"
puts string[1..string.length] -> outputs "ellow"
Is there a shorter way to get Ruby to do the same thing without using string.length? In Python, for example, we can write string[1:] and it will do it.
Thanks in advance.
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.
While using
[]in Ruby to get the index of a String or an Array, using a negative number will start the counting from the end of the index starting with1([-0]will get you the same as[0]).In your example,
will output the desired string
"ellow".Equally,
will produce
"ello"and so on.Documentation for String#[]