Michael
Michael

Reputation: 7377

Sequences in Ruby

Is there a simple function in ruby to create sequences? For example, I want a sequence from 1 to 100 incrementing by 3. So

Function(1,100,increment = 3) = [1,4,7,10, ...,97,100]

Thanks!

Upvotes: 16

Views: 15928

Answers (1)

Jokester
Jokester

Reputation: 5617

Range#step generates another enumerator with given step.

say (1..100).step(3).to_a would be [1,4,7, ... , 97, 100]

alternatively Numeric#step(limit,step) does similar things,

say 1.step(100,3).to_a

Upvotes: 37

Related Questions