Skip to content

Commit

Permalink
Range#step returns correct ArithmeticSequence
Browse files Browse the repository at this point in the history
  • Loading branch information
ccocchi committed Oct 28, 2021
1 parent b45ce7a commit cd3b2cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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 (#2516)

Compatibility:

Expand Down
18 changes: 18 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,24 @@
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

context "when begin and end are not numerics" do
it "returns an instance of Enumerator" do
("a".."z").step.class.should == Enumerator
Expand Down
2 changes: 1 addition & 1 deletion src/main/ruby/truffleruby/core/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def %(n)

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))
if !block_given? && (Primitive.nil?(self.begin) || 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

Expand Down

0 comments on commit cd3b2cb

Please sign in to comment.