Skip to content

Commit

Permalink
[GR-18163] Range#step with begin-less range returns correct Arithmeti…
Browse files Browse the repository at this point in the history
…cSequence (#2516)

PullRequest: truffleruby/3017
  • Loading branch information
eregon committed Oct 28, 2021
2 parents 789fcdf + 2566e8f commit ede71a4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Bug fixes:
* Capture the intercepted feature path during patching to reuse during patch require (#2441).
* Update `Module#constants` to filter invalid constant identifiers (#2452).
* Fixed `-0.0 <=> 0.0` and `-0.0 <=> 0` to return `0` like on CRuby (#1391).
* Fixed `Range#step` to return correct class with begin-less range (@ccocchi, #2516).

Compatibility:

Expand Down
26 changes: 26 additions & 0 deletions spec/ruby/core/range/step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,32 @@
end
end

ruby_version_is "2.7" do
context "when begin is not defined and end is numeric" do
it "returns an instance of Enumerator::ArithmeticSequence" do
eval("(..10)").step.class.should == Enumerator::ArithmeticSequence
end
end
end

context "when range is endless" do
it "returns an instance of Enumerator::ArithmeticSequence when begin is numeric" do
(1..).step.class.should == Enumerator::ArithmeticSequence
end

it "returns an instance of Enumerator when begin is not numeric" do
("a"..).step.class.should == Enumerator
end
end

ruby_version_is "2.7" do
context "when range is beginless and endless" do
it "returns an instance of Enumerator" do
Range.new(nil, nil).step.class.should == Enumerator
end
end
end

context "when begin and end are not numerics" do
it "returns an instance of Enumerator" do
("a".."z").step.class.should == Enumerator
Expand Down
14 changes: 3 additions & 11 deletions src/main/ruby/truffleruby/core/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,26 +437,18 @@ def minmax(&block)
end

def %(n)
step(n)
Truffle::RangeOperations.step_no_block(self, n)
end

private def step_internal(step_size=1, &block) # :yields: object

if !block_given? && Primitive.object_kind_of?(self.begin, Numeric) && (Primitive.nil?(self.end) || Primitive.object_kind_of?(self.end, Numeric))
return Enumerator::ArithmeticSequence.new(self, :step, self.begin, self.end, step_size, self.exclude_end?)
end

return to_enum(:step, step_size) do
validated_step_args = Truffle::RangeOperations.validate_step_size(self.begin, self.end, step_size)
Truffle::RangeOperations.step_iterations_size(self, *validated_step_args)
end unless block_given?
return Truffle::RangeOperations.step_no_block(self, step_size) unless block

values = Truffle::RangeOperations.validate_step_size(self.begin, self.end, step_size)
first = values[0]
last = values[1]
step_size = values[2]

return step_endless(first, step_size, &block) if Primitive.nil? last
return step_endless(first, step_size, &block) if Primitive.nil?(last)

case first
when Float
Expand Down
19 changes: 19 additions & 0 deletions src/main/ruby/truffleruby/core/truffle/range_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@

module Truffle
module RangeOperations
def self.step_no_block(range, step_size)
from, to = range.begin, range.end
if arithmetic_range?(from, to)
Enumerator::ArithmeticSequence.new(range, :step, from, to, step_size, range.exclude_end?)
else
to_enum(:step, step_size) do
validated_step_args = validate_step_size(from, to, step_size)
step_iterations_size(range, *validated_step_args)
end
end
end

def self.arithmetic_range?(from, to)
if Primitive.object_kind_of?(from, Numeric)
Primitive.object_kind_of?(to, Numeric) || Primitive.nil?(to)
else
Primitive.nil?(from) && Primitive.object_kind_of?(to, Numeric)
end
end

def self.step_iterations_size(range, first, last, step_size)
case first
Expand Down

0 comments on commit ede71a4

Please sign in to comment.