https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112011-01-07T04:28:44ZRuby Issue Tracking SystemRuby master - Bug #4245: Array index with Range inconsistencyhttps://bugs.ruby-lang.org/issues/4245?journal_id=150272011-01-07T04:28:44Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>=begin<br>
This is per spec.</p>
<p>Think of it this way:</p>
<p>[5..-1]: give me all elements after the 5th one and up to the last one. There are none, so []<br>
[6..-1]: give me all elements after the 6th one and up to the last one. Say what? There is no 6th one, so returns nil as the given request is out of bounds.</p>
<p>=end</p> Ruby master - Bug #4245: Array index with Range inconsistencyhttps://bugs.ruby-lang.org/issues/4245?journal_id=150372011-01-07T19:42:28Zsdsykes (Stephen Sykes)sdsykes@gmail.com
<ul></ul><p>=begin<br>
Yes, it's a bit odd, but it is clearly noted in the docs that this special case exists.</p>
<p>Marc-Andre: - For me at least I'm not sure that thinking about it as 'the element after the nth one' is totally helpful - eg:</p>
<blockquote>
<p>a = []<br>
=> []<br>
a[1..1]<br>
=> nil<br>
a[0..0]<br>
=> []</p>
</blockquote>
<p>But whichever way you think about it, this is how it works.</p>
<p>From memory (it has been raised before) I think the reason was that it can make handling end conditions easier if an empty array is returned rather than nil if you hit the first non existing element after the end of the array.</p>
<p>=end</p>