Array indexing

D

Duke Normandin

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?
 
7

7stud --

Duke Normandin wrote in post #994657:
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?

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?
the first _says_: "start at index0 & gimme 3"
the latter _say_: "start at index0 and goto index2"

Where is the ruby documentation located?

If the data requirements start at >index0, then there is no offset
between the "gimme" and the 'goto".

Am I getting it?

No.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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?
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


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.
 
D

Duke Normandin

hey ...

I want to make sure that I understand the subtle difference in the
syntax.
[snip]
IDK, but a quick study of behaviour should make it very obvious:

[snip the good stuff]

Thanks for _all_ that!

However, I think that I summarized it in my original post, exactly as
you are showing it above. My definition of "gimme" and "goto" may not
be as clear as what I had hoped it would be. No matter! I'll check my
understanding of the issue against what you've provided above. Much
obliged!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top