Ruby idiom for substring from index until end of string

Ruby 2.6 introduced endless ranges, which basically remove the need to have to specify the end index. In your case, you can do:

s = "hello world"
s[6..]

I think it isn't.

It seems that Range is better way to do it.


Here is 'nicer' if you wish. You can extend ruby String class and then use this method into your code. For example:

class String
  def last num
    self[-num..-1]
  end
end

And then:

s = "hello world"
p s.last(6)

To get the string from a range:

s = 'hello world'
s[5..s.length - 1] # world

However, if you just want to get the last word:

'hello world'.split(' ').last # world