From: Josh Cheek Date: 2011-04-24T02:37:44+09:00 Subject: Re: Array indexing --bcaec548610a60ad8104a1996e89 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Apr 23, 2011 at 11:22 AM, Duke Normandin wrote: > hey ... > > I want to make sure that I understand the subtle difference in the > syntax. > > my_array[0,3] > > and > > my_array[0..2] > > both will return the same array elements - correct? > > the first _says_: "start at index0 & gimme 3" > the latter _say_: "start at index0 and goto index2" > > If the data requirements start at >index0, then there is no offset > between the "gimme" and the 'goto". > > Am I getting it? > -- > Duke > > IDK, but a quick study of behaviour should make it very obvious: numbers = *0...10 # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] numbers[0,3] # => [0, 1, 2] numbers[1,3] # => [1, 2, 3] numbers[2,3] # => [2, 3, 4] numbers[3,3] # => [3, 4, 5] numbers[0,4] # => [0, 1, 2, 3] numbers[1,4] # => [1, 2, 3, 4] numbers[2,4] # => [2, 3, 4, 5] numbers[3,4] # => [3, 4, 5, 6] numbers[0..5] # => [0, 1, 2, 3, 4, 5] numbers[1..5] # => [1, 2, 3, 4, 5] numbers[2..5] # => [2, 3, 4, 5] numbers[3..5] # => [3, 4, 5] numbers[3..7] # => [3, 4, 5, 6, 7] numbers[4..7] # => [4, 5, 6, 7] numbers[5..7] # => [5, 6, 7] numbers[6..7] # => [6, 7] numbers[7..7] # => [7] If you're using TextMate, you can update the comments with command+control+shift+e. Otherwise, you might try entering each line into irb, and it will update for you the correct value. Or you could write a program to cycle through inputs and print the values, something like: numbers = *0...10 (0..3).each do |start| result = numbers[start,3] puts "numbers[#{start},3] # => #{result.inspect}" end On Sat, Apr 23, 2011 at 12:02 PM, 7stud -- wrote: > > If someone had a gun to your head and told you that you had 30 seconds > to provide an answer to that question, what would you do? > > Ruby is serious business. --bcaec548610a60ad8104a1996e89--