Range indexes are negative : char range « String « Ruby






Range indexes are negative


#!/usr/bin/env ruby

thoreau = "If a man does not keep pace with his companions, perhaps it is because he hears a different drummer."

puts thoreau[-8..-2] # => "drummer"

 








Related examples in the same category

1.Retrieve part of a string with the [] method, using a range.
2.starting at the end of the string using negative numbers, get the second to last character through the eighth to last
3.iterate over all the characters using a block