Skip to content

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

Closed
wants to merge 2 commits into from

1 participant

@ferrous26

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]
ferrous26 added some commits Mar 25, 2012
@ferrous26 ferrous26 Ignore confest files generated by autoconf 5156447
@ferrous26 ferrous26 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
@ferrous26 ferrous26 closed this Oct 12, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.