1290 1290 - 3 years ago 123
Ruby Question

Does slice and shift do 0 based counting on the array?

In this example below:

a = [1,2,3,4,5]
a.slice(1,3)


In the slice method we are saying to start at position 1 (which is
2
in the array). Then continue for 3 elements. What I don't get is when we continue for 3 elements are we counting the start position or not? I know this is a dumb question but it doesn't say on the docs.

Answer Source

Yes, apparently, according to the CLI

a = [1,2,3,4,5] => [1, 2, 3, 4, 5] 
a.slice(1,3)    => [2, 3, 4] 

You should install the CLI to play around, its great to try things like this out

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download