【发布时间】:2011-04-07 16:25:13
【问题描述】:
可能重复:
Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)
我一直在玩 ruby 中的数组切片,但我不明白下面的最后 2 个结果:
a = [1,2,3]
a[2,1] # output is [3]
a[3,1] # output is [] ...why??
a[4,1] # output is nil ...according to the docs this makes sense
为什么a[3,1] 是一个空数组,而a[4,1] 是零?
如果有的话,我希望a[3,1] 也会返回 nil。根据ruby docs,如果起始索引超出范围,则数组拆分应返回 nil。那么为什么a[3,1] 没有返回零呢?
注意:这是在 ruby 1.9.2 上
【问题讨论】:
标签: ruby-on-rails ruby