Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Range#step for non-integer Steppable types #11130

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec/std/range_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ describe "Range" do
it_iterates "begin.succ == end inclusive", [1, 2] of Int32, (1..2).step(1)
it_iterates "begin.succ == end exclusive", [1] of Int32, (1...2).step(1)

it_iterates "Float step", [1.0, 1.5, 2.0, 2.5, 3.0], (1..3).step(by: 0.5)
it_iterates "Time::Span step", [1.minutes, 2.minutes, 3.minutes], (1.minutes..3.minutes).step(by: 1.minutes)

describe "with #succ type" do
range_basic = RangeSpecIntWrapper.new(1)..RangeSpecIntWrapper.new(5)
it_iterates "basic", [1, 2, 3, 4, 5].map(&->RangeSpecIntWrapper.new(Int32)), range_basic.step
Expand Down
12 changes: 6 additions & 6 deletions src/range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ struct Range(B, E)
raise ArgumentError.new("Can't step beginless range")
end

if current.is_a?(Steppable)
{% if B < Steppable %}
current.step(to: @end, by: by, exclusive: @exclusive) do |x|
yield x
end
else
{% else %}
end_value = @end
while end_value.nil? || current < end_value
yield current
Expand All @@ -253,7 +253,7 @@ struct Range(B, E)
end
end
yield current if !@exclusive && current == @end
end
{% end %}
end

# :ditto:
Expand All @@ -263,11 +263,11 @@ struct Range(B, E)
raise ArgumentError.new("Can't step beginless range")
end

if start.is_a?(Steppable)
{% if B < Steppable %}
start.step(to: @end, by: by, exclusive: @exclusive)
else
{% else %}
StepIterator(self, B, typeof(by)).new(self, by)
end
{% end %}
end

# Returns `true` if this range excludes the *end* element.
Expand Down