From: "John Lam (CLR)" Date: 2007-07-07T16:00:35+09:00 Subject: Re: Array#values_at bug? There's also similar behavior in other range-like entities in strings: "foo"[3,0] == "" But "foo"[4,0] == nil Do folks depend on this kind of behavior? I'll need to add a special case to my string implementation to replicate this behavior in IronRuby. Or should I do the correct thing and make "foo"[3,0] == nil? Thanks, -John -----Original Message----- From: yuanyi zhang [mailto:zhangyuanyi@gmail.com] Sent: Tuesday, July 03, 2007 6:07 PM To: ruby-core@ruby-lang.org Subject: Re: Array#values_at bug? 2007/7/3, John Lam (CLR) : > > > > > a = [1,2,3,4] > > > > a[2..4].should == [3,4] > > > > but > > > > a.values_at(2..4).should == [3,4,nil] > > > > The latter behavior does not feel consistent at all, especially since > > > > a.values_at(2..5).should == [3,4,nil] > > > > IronRuby right now just calls slice() in a loop to implement values_at. Is > this what MRI does? > > Array is a built-in class in MRI, and is implemented by C. You can find the source in array.c, it's called rb_values_at. The reason why a.values_at(2..5) == a.values_at(2..4) is because rb_range_beg_len consider range(2..4) and range(2..5) has same len. You can find rb_range_beg_len in Range.c: if (end > len) end = len; This sentence guarantee Range end won't be greater then Array length, But I think it should let end = len-1, then a.values_at(2..4).should == [3,4] Hope this help. BR Yuanyi Zhang