about ruby range?

后端 未结 3 1762
抹茶落季
抹茶落季 2020-12-20 11:50

like this

range = (0..10)

how can I get number like this:

0 5 10 

plus five every time but less than 10

3条回答
  •  青春惊慌失措
    2020-12-20 12:13

    Try using .step() to go through at a given step.

    (0..20).step(5) do |n|
        print n,' '
    end
    

    gives...

    0 5 10 15 20
    

    As mentioned by dominikh, you can add .to_a on the end to get a storable form of the list of numbers: (0..20).step(5).to_a

提交回复
热议问题