Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Range#%

rng.step(n=1) {| obj | block } → rng
rng.step(n=1) → an_enumerator
rng.step(n=1) → an_arithmetic_sequence
rng % n → an_enumerator
rng % n → an_arithmetic_sequence

Iterates over the range, passing each nth element to the block. If begin and end are numeric, n is added for each iteration. Otherwise step invokes succ to iterate through range elements.

If no block is given, an enumerator is returned instead. Especially, the enumerator is an Enumerator::ArithmeticSequence if begin and end of the range are numeric.

Example
range = Xs.new(1)..Xs.new(10)
range.step(2) {|x| puts x}
puts
range.step(3) {|x| puts x}

produces:

 1 x
 3 xxx
 5 xxxxx
 7 xxxxxxx
 9 xxxxxxxxx

 1 x
 4 xxxx
 7 xxxxxxx
10 xxxxxxxxxx

See Range for the definition of class Xs.

See also .new #== .json_create #=== also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1