Skip to content

Commit

Permalink
Extract logic for Range#step with no block
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 28, 2021
1 parent 820e2d4 commit 2566e8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/main/ruby/truffleruby/core/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,23 +437,13 @@ 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
from, to = self.begin, self.end
unless block
if Truffle::RangeOperations.arithmetic_range?(from, to)
return Enumerator::ArithmeticSequence.new(self, :step, from, to, step_size, self.exclude_end?)
else
return to_enum(:step, step_size) do
validated_step_args = Truffle::RangeOperations.validate_step_size(from, to, step_size)
Truffle::RangeOperations.step_iterations_size(self, *validated_step_args)
end
end
end
return Truffle::RangeOperations.step_no_block(self, step_size) unless block

values = Truffle::RangeOperations.validate_step_size(from, to, step_size)
values = Truffle::RangeOperations.validate_step_size(self.begin, self.end, step_size)
first = values[0]
last = values[1]
step_size = values[2]
Expand Down
12 changes: 12 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,18 @@

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)
Expand Down

0 comments on commit 2566e8f

Please sign in to comment.