array ending syntax question

R

Roger Pack

irb(main):001:0> [1,2][2..2]
=> []
irb(main):002:0> [1,2][3..3]
=> nil


Is this expected?
the ri says:
Returns nil if the index (or starting index) are out of range.

does the first example match this?
Thanks!
-R
 
F

fedzor

irb(main):001:0> [1,2][2..2]
=> []
irb(main):002:0> [1,2][3..3]
=> nil


Is this expected?
the ri says:
Returns nil if the index (or starting index) are out of range.

does the first example match this?

Yes, because array indices start at 0. So the highest index in the
example is 1.

-------------------------------------------------------|
~ Ari
Some people want love
Others want money
Me... Well...
I just want this code to compile
 
A

Alex Gutteridge

irb(main):001:0> [1,2][2..2]
=> []
irb(main):002:0> [1,2][3..3]
=> nil


Is this expected?
the ri says:
Returns nil if the index (or starting index) are out of range.

does the first example match this?
Thanks!
-R

From the Pickaxe: If the start index equals the array size and a
length or range (as here) parameter is given, an empty array is
returned.

Not sure why you would want that behaviour though...

Alex Gutteridge

Department of Biochemistry
University of Cambridge
 
F

Frederick Cheung

irb(main):001:0> [1,2][2..2]
=> []
irb(main):002:0> [1,2][3..3]
=> nil


Is this expected?
the ri says:
Returns nil if the index (or starting index) are out of range.

does the first example match this?
Thanks!
-R

From the Pickaxe: If the start index equals the array size and a
length or range (as here) parameter is given, an empty array is
returned.

Not sure why you would want that behaviour though...


It sort of makes sense in that in an n item array n is actually a
useful index - it's where the next item will go.

Fred
 

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

Similar Threads

Inconsistency in Array#[] 13
parentheses and newlines 2
Array#slice! bug? 0
array question 32
array help 8
Outputting signal values to terminal Within Character Array 0
Help with array 1
Class instance method 2

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top