Ruby idiom for substring from index until end of string

Sherwin Yu picture Sherwin Yu · Feb 13, 2013 · Viewed 29.7k times · Source

Just wondering if there's a Ruby idiom for extracting a substring from an index until the end of the string. I know of str[index..-1] which works by passing in a range object to the String's [] method but it's a little clunky. In python, for example, you could write str[index:] which would implicitly get you the rest of the string.

Example:

s = "hello world"
s[6..-1] # <-- "world"

Is there anything nicer than s[6..-1]?

Answer

Paul Brit picture Paul Brit · Feb 13, 2013

I think it isn't.

It seems that Range is better way to do it.