Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make rb_range_beg_len handle index past end of length when range is n…
…ot exclusive

This handles an edge case in Array#values_at:

[0,1,2,3,4,5].values_at 4..5
[0,1,2,3,4,5].values_at 4..6
[0,1,2,3,4,5].values_at 4..7

Previously, the second and third case would include a nil
at the end of the array.

It is important to note that this change is only triggered
when the err parameter is set to 0 or 2, since there are
cases (i.e. Array#fill) where we do not want to adjust the
range.
  • Loading branch information
ferrous26 committed Mar 26, 2012
commit 51b79f7a15ea9fb27a2d82edb8f887cd23a92056
4 changes: 3 additions & 1 deletion range.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,10 @@ rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
if (err == 0 || err == 2) {
if (beg > len)
goto out_of_range;
if (end == len && !excl)
end -= 1;
if (end > len)
end = len;
end = excl ? len : len - 1;
}
if (end < 0)
end += len;
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,12 @@ def test_values_at
assert_equal(@cls['a', 'c', 'e'], a.values_at(0, 2, 4))
assert_equal(@cls['j', 'h', 'f'], a.values_at(-1, -3, -5))
assert_equal(@cls['h', nil, 'a'], a.values_at(-3, 99, 0))
assert_equal(@cls['h', 'i', 'j'], a.values_at(7..9))
assert_equal(@cls['h', 'i', 'j'], a.values_at(7..10))
assert_equal(@cls['h', 'i', 'j'], a.values_at(7..11))
assert_equal(@cls['h', 'i', 'j'], a.values_at(7...10))
assert_equal(@cls['h', 'i', 'j'], a.values_at(7...11))
assert_equal(@cls['h', 'i', 'j'], a.values_at(7...12))
end

def test_join
Expand Down