Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Change Array#values_at to handle range index past end of array properly #109

Closed
wants to merge 2 commits into from

1 participant

Mark Rada
Mark Rada

This refers to Redmine #6203:

This changes Array#vaules_at to behave more consistently with array slicing.

Without this patch:

    a = [0,1,2,3,4,5]
    a[4..6] # => [4, 5]
    a.values_at(4..6) # => [4,5,nil]
    a.values_at(4..600) # => [4,5,nil]

With this patch:

    a.values_at 4..6 # => [4, 5]
    a.values_at 4..600 # => [4, 5]
added some commits March 25, 2012
Mark Rada Ignore confest files generated by autoconf 5156447
Mark Rada 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.
51b79f7
Mark Rada ferrous26 closed this October 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Mar 26, 2012
Mark Rada Ignore confest files generated by autoconf 5156447
Mark Rada 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.
51b79f7
This page is out of date. Refresh to see the latest.
1  .gitignore
@@ -43,6 +43,7 @@ y.tab.c
43 43
 /automake
44 44
 /beos
45 45
 /breakpoints.gdb
  46
+/conftest
46 47
 /config.cache
47 48
 /config.h
48 49
 /config.h.in
4  range.c
@@ -749,8 +749,10 @@ rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
749 749
     if (err == 0 || err == 2) {
750 750
 	if (beg > len)
751 751
 	    goto out_of_range;
  752
+	if (end == len && !excl)
  753
+	    end -= 1;
752 754
 	if (end > len)
753  
-	    end = len;
  755
+	    end = excl ? len : len - 1;
754 756
     }
755 757
     if (end < 0)
756 758
 	end += len;
6  test/ruby/test_array.rb
@@ -888,6 +888,12 @@ def test_values_at
888 888
     assert_equal(@cls['a', 'c', 'e'], a.values_at(0, 2, 4))
889 889
     assert_equal(@cls['j', 'h', 'f'], a.values_at(-1, -3, -5))
890 890
     assert_equal(@cls['h', nil, 'a'], a.values_at(-3, 99, 0))
  891
+    assert_equal(@cls['h', 'i', 'j'], a.values_at(7..9))
  892
+    assert_equal(@cls['h', 'i', 'j'], a.values_at(7..10))
  893
+    assert_equal(@cls['h', 'i', 'j'], a.values_at(7..11))
  894
+    assert_equal(@cls['h', 'i', 'j'], a.values_at(7...10))
  895
+    assert_equal(@cls['h', 'i', 'j'], a.values_at(7...11))
  896
+    assert_equal(@cls['h', 'i', 'j'], a.values_at(7...12))
891 897
   end
892 898
 
893 899
   def test_join
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.