新手问题 数组取出子元素问题

autumnwolf · November 21, 2014 · Last by autumnwolf replied at November 21, 2014 · 1691 hits

为什么 3,4 时候返回时空数组,5 就返回了 nil?

   array = [:peanut, :butter, :and, :jelly]
 => [:peanut, :butter, :and, :jelly] 
2.1.2 :055 > array[3,0]
 => [] 
2.1.2 :056 > array[4,0]
 => [] 
2.1.2 :057 > array[5,0]
 => nil 
2.1.2 :058 > 

嗯,一个坑

关于Array#slice an empty array is returned when the starting index for an element range is at the end of the array. Returns nil if the index (or starting index) are out of range. 来源 ruby-doc

You need to Sign in before reply, if you don't have an account, please Sign up first.