Skip to content

Commit 07a2e60

Browse files
committed
Raise TypeError instead of ArgumentError when Array#fill's 3d argument length is not Number
1 parent d1f8516 commit 07a2e60

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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)