Skip to content

Commit 4693f7b

Browse files
committed
[GR-18163] Raise TypeError instead of ArgumentError when Array#fill's 3d argument length is not Number
PullRequest: truffleruby/3380
2 parents d1f8516 + 01b6e52 commit 4693f7b

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Bug fixes:
88

99
Compatibility:
1010

11+
* Fix `Array#fill` to raise `TypeError` instead of `ArgumentError` when the length argument is not numeric (#2652, @andrykonchin).
1112

1213
Performance:
1314

spec/ruby/core/array/fill_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@
205205
-> { [].fill('a', obj) }.should raise_error(TypeError)
206206
end
207207

208+
it "raises a TypeError when the length is not numeric" do
209+
-> { [1, 2, 3].fill("x", 1, "foo") }.should raise_error(TypeError, /no implicit conversion of String into Integer/)
210+
-> { [1, 2, 3].fill("x", 1, :"foo") }.should raise_error(TypeError, /no implicit conversion of Symbol into Integer/)
211+
-> { [1, 2, 3].fill("x", 1, Object.new) }.should raise_error(TypeError, /no implicit conversion of Object into Integer/)
212+
end
213+
208214
not_supported_on :opal do
209215
it "raises an ArgumentError or RangeError for too-large sizes" do
210216
error_types = [RangeError, ArgumentError]

src/main/ruby/truffleruby/core/array.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,7 @@ def fetch(idx, default=undefined)
455455
left = 0 if left < 0
456456

457457
if !Primitive.undefined?(length) and length
458-
begin
459-
right = Primitive.rb_num2int length
460-
rescue TypeError
461-
raise ArgumentError, 'second argument must be an Integer'
462-
end
463-
458+
right = Primitive.rb_num2int length
464459
return self if right == 0
465460
right += left
466461
else

0 commit comments

Comments
 (0)