Navigation Menu

Skip to content

Commit

Permalink
Make Array#[]= raise IndexError.
Browse files Browse the repository at this point in the history
If second param is negative, Array#[] raise IndexError.
  • Loading branch information
yui-knk committed May 7, 2014
1 parent 09b9c77 commit eafc4dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/array.c
Expand Up @@ -561,6 +561,10 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val
mrb_int i, argc;

ary_modify(mrb, a);

/* len check */
if (len < 0) mrb_raisef(mrb, E_INDEX_ERROR, "negative length (%S)", mrb_fixnum_value(len));

/* range check */
if (head < 0) {
head += a->len;
Expand Down
5 changes: 5 additions & 0 deletions test/t/array.rb
Expand Up @@ -66,6 +66,11 @@
# this will cause an exception due to the wrong arguments
a.[]=(1,2,3,4)
end
assert_raise(IndexError) do
# this will cause an exception due to the wrong arguments
a = [1,2,3,4,5]
a[1, -1] = 10
end

assert_equal(4, [1,2,3].[]=(1,4))
assert_equal(3, [1,2,3].[]=(1,2,3))
Expand Down

0 comments on commit eafc4dd

Please sign in to comment.