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