Skip to content

Commit 9ef54d2

Browse files
author
Nicolas Laurent
committed
implement to_a for endless ranges
1 parent 5a119b7 commit 9ef54d2

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

spec/ruby/core/array/to_a_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
array = ArraySpecs.recursive_array
2222
array.to_a.should == array
2323
end
24+
25+
it "throws an exception for endless ranges" do
26+
-> { (1..).to_a }.should raise_error(RangeError)
27+
end
2428
end

src/main/java/org/truffleruby/core/range/RangeNodes.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ protected DynamicObject toA(DynamicObject range) {
386386
}
387387
}
388388

389-
@Specialization(guards = "isObjectRange(range)")
390-
protected Object toA(VirtualFrame frame, DynamicObject range) {
389+
@Specialization(guards = { "isObjectRange(range)", "!isEndlessRange(getContext(), range)" })
390+
protected Object boundedToA(VirtualFrame frame, DynamicObject range) {
391391
if (toAInternalCall == null) {
392392
CompilerDirectives.transferToInterpreterAndInvalidate();
393393
toAInternalCall = insert(CallDispatchHeadNode.createPrivate());
@@ -396,6 +396,13 @@ protected Object toA(VirtualFrame frame, DynamicObject range) {
396396
return toAInternalCall.call(range, "to_a_internal");
397397
}
398398

399+
@Specialization(guards = { "isObjectRange(range)", "isEndlessRange(getContext(), range)" })
400+
protected Object endlessToA(VirtualFrame frame, DynamicObject range) {
401+
throw new RaiseException(getContext(), coreExceptions().rangeError(
402+
"cannot convert endless range to an array",
403+
this));
404+
}
405+
399406
}
400407

401408
/**

0 commit comments

Comments
 (0)