Implements a #step method for iterating from a value.
Iterates from self to limit incrementing by the amount of step on each iteration.
Iterates from self to limit incrementing by the amount of step on each iteration.
Iterates from self to limit incrementing by the amount of step on each iteration. If exclusive is true, limit is excluded from the iteration.
ary = [] of Int32 1.step(to: 4, by: 2) do |x| ary << x end ary # => [1, 3] 1.step(to: 4, by: 2).to_a # => [1, 3] 1.step(to: 4, by: 1).to_a # => [1, 2, 3, 4] 1.step(to: 4, by: 1, exclusive: true).to_a # => [1, 2, 3]
The type of each iterated element is typeof(self + step).
If to is nil, iteration is open ended.
The starting point (self) is always iterated as first element, with two exceptions:
self and to don't compare (i.e. (self <=> to).nil?). Example: 1.0.step(Float::NAN)
by. Example: 1.step(to: 2, by: -1)
In those cases the iteration is empty.
Iterates from self to limit incrementing by the amount of step on each iteration. If exclusive is true, limit is excluded from the iteration.
ary = [] of Int32 1.step(to: 4, by: 2) do |x| ary << x end ary # => [1, 3] 1.step(to: 4, by: 2).to_a # => [1, 3] 1.step(to: 4, by: 1).to_a # => [1, 2, 3, 4] 1.step(to: 4, by: 1, exclusive: true).to_a # => [1, 2, 3]
The type of each iterated element is typeof(self + step).
If to is nil, iteration is open ended.
The starting point (self) is always iterated as first element, with two exceptions:
self and to don't compare (i.e. (self <=> to).nil?). Example: 1.0.step(Float::NAN)
by. Example: 1.step(to: 2, by: -1)
In those cases the iteration is empty.
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/Steppable.html