Array Slicing 1.9.2

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.

On Sat, Jan 29, 2011 at 6:45 AM, Lucky D.
[email protected]wrote:

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.

thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs