Skip to content

Latest commit

 

History

History
16 lines (12 loc) · 486 Bytes

step.md

File metadata and controls

16 lines (12 loc) · 486 Bytes

Array#step

require 'facets/array/step'

Iterate over every nth element of an array. Without a block, it returns an Enumerator.

r = []
[:a, :b, :c, :d].step(2) { |x| r << x }
r.assert == [:b, :d]

[:a, :b, :c, :d].step(1).to_a.assert == [:a, :b, :c, :d]
[:a, :b, :c, :d].step(2).to_a.assert == [:b, :d]
[:a, :b, :c, :d].step(3).to_a.assert == [:c]
[:a, :b, :c, :d].step(4).to_a.assert == [:d]
[:a, :b, :c, :d].step(5).to_a.assert == []