step

rng.step(n=1) {| obj | block } â rng
rng.step(n=1) â an_enumerator
Instance Public methods

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.

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

produces:

1
2
3
4
5
6
7
8
9
10
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.

doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.