views:

45

answers:

1

Hi,

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.

+8  A: 

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.

Matt Greer
it makes much more sense now thanks
Mo