Array Slicing 1.9.2

L

Lucky Dev

I'm using ruby 1.9.2 Today i was playing around array and here is the
console log.


ruby-1.9.2-p0 > a = (10..15).to_a
=> [10, 11, 12, 13, 14, 15]
ruby-1.9.2-p0 > a[0]
=> 10
ruby-1.9.2-p0 > a[5]
=> 15
ruby-1.9.2-p0 > a[6]
=> nil
ruby-1.9.2-p0 > a[5,1]
=> [15]
ruby-1.9.2-p0 > a[5,2]
=> [15]
ruby-1.9.2-p0 > a[5,3]
=> [15]
ruby-1.9.2-p0 > a[6,1]
=> []
ruby-1.9.2-p0 > a[6,2]
=> []
ruby-1.9.2-p0 > a[6,3]
=> []
ruby-1.9.2-p0 > a[6]
=> nil
ruby-1.9.2-p0 > a[7]
=> nil
ruby-1.9.2-p0 > a[7,1]
=> nil
ruby-1.9.2-p0 > a[7,2]
=> nil
ruby-1.9.2-p0 > a[7,3]
=> nil


I've created an array with 6 elements in it with indexes from 0 to 5.
a[5] exists while a[6] does not.

a[6,1] or a[6,n] should give 1 or n elements from 6th index. There is no
element in 6th index. so it must return a nil.
It should be the same for a[7,1] or a[7,n] and for this a[7,n] it
returns nil.

Both a[6] a[7] does not exist in array. can you explain why there is a
difference in slicing output above.
 
A

Adam Prescott

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

I'm using ruby 1.9.2 Today i was playing around array and here is the
console log.

[...]

a[6,1] or a[6,n] should give 1 or n elements from 6th index. There is no
element in 6th index. so it must return a nil.
It should be the same for a[7,1] or a[7,n] and for this a[7,n] it
returns nil.

Both a[6] a[7] does not exist in array. can you explain why there is a
difference in slicing output above.

There was a discussion about this that you can read:
http://www.ruby-forum.com/topic/216796

It's not actually specific to 1.9, if you're wondering.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top