ruby-changes:46578
From: nobu <ko1@a...>
Date: Sat, 13 May 2017 02:23:52 +0900 (JST)
Subject: [ruby-changes:46578] nobu:r58694 (trunk): array.c: fix position in message
nobu 2017-05-13 02:23:46 +0900 (Sat, 13 May 2017) New Revision: 58694 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58694 Log: array.c: fix position in message * array.c (rb_ary_insert): fix the position in error message, when it is less than -1. Modified files: trunk/array.c trunk/test/ruby/test_array.rb Index: array.c =================================================================== --- array.c (revision 58693) +++ array.c (revision 58694) @@ -1786,7 +1786,12 @@ rb_ary_insert(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/array.c#L1786 if (pos == -1) { pos = RARRAY_LEN(ary); } - if (pos < 0) { + else if (pos < 0) { + long minpos = -RARRAY_LEN(ary) - 1; + if (pos < minpos) { + rb_raise(rb_eIndexError, "index %ld too small for array; minimum: %ld", + pos, minpos); + } pos++; } rb_ary_splice(ary, pos, 0, argv + 1, argc - 1); Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 58693) +++ test/ruby/test_array.rb (revision 58694) @@ -2124,6 +2124,7 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2124 assert_raise(TypeError) { a.insert(Object.new) } assert_equal([0, 1, 2], a.insert(-1, 2)) assert_equal([0, 1, 3, 2], a.insert(-2, 3)) + assert_raise_with_message(IndexError, /-6/) { a.insert(-6, 4) } assert_raise(RuntimeError) { [0].freeze.insert(0)} assert_raise(ArgumentError) { [0].freeze.insert } end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/