Content-Length: 334715 | pFad | http://github.com/jruby/jruby/commit/d436a0ecc6fc80537d8b88fa8cdfb8ab1ae07ff1

89 Merge pull request #8904 from headius/always_check_array_insert_pos · jruby/jruby@d436a0e · GitHub
Skip to content

Commit d436a0e

Browse files
authored
Merge pull request #8904 from headius/always_check_array_insert_pos
Always check array insert pos
2 parents 9c44c1d + 9555619 commit d436a0e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

core/src/main/java/org/jruby/RubyArray.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,18 +1200,27 @@ public IRubyObject insert19(IRubyObject arg1, IRubyObject arg2) {
12001200

12011201
@JRubyMethod(name = "insert", required = 1, rest = true, checkArity = false)
12021202
public IRubyObject insert(IRubyObject[] args) {
1203+
switch (args.length) {
1204+
case 0:
1205+
return insert();
1206+
case 1:
1207+
return insert(args[0]);
1208+
case 2:
1209+
return insert(args[0], args[1]);
1210+
}
1211+
12031212
final Ruby runtime = metaClass.runtime;
12041213

12051214
int argc = Arity.checkArgumentCount(runtime, args, 1, -1);
12061215

12071216
modifyCheck();
12081217

1218+
long pos = RubyNumeric.num2long(args[0]);
1219+
12091220
if (argc == 1) return this;
12101221

12111222
unpack();
12121223

1213-
long pos = RubyNumeric.num2long(args[0]);
1214-
12151224
if (pos == -1) pos = realLength;
12161225
if (pos < 0) pos++;
12171226

spec/ruby/core/array/insert_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
[].insert(-2).should == []
5858
end
5959

60+
# https://bugs.ruby-lang.org/issues/13558
61+
it "attempts to coerce a lone position argument" do
62+
lambda { [].insert(Object.new) }.should raise_error(TypeError)
63+
end
64+
6065
it "tries to convert the passed position argument to an Integer using #to_int" do
6166
obj = mock('2')
6267
obj.should_receive(:to_int).and_return(2)

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/jruby/jruby/commit/d436a0ecc6fc80537d8b88fa8cdfb8ab1ae07ff1

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy