Create array of n items based on integer value

后端 未结 5 628
小鲜肉
小鲜肉 2020-12-13 11:58

Given I have an integer value of, e.g., 10.

How can I create an array of 10 elements like [1,2,3,4,5,6,7,8,9,10]?

5条回答
  •  感动是毒
    2020-12-13 12:28

    You can do this:

    array= Array(0..10)
    

    If you want to input, you can use this:

    puts "Input:"
    n=gets.to_i
    array= Array(0..n)
    puts array.inspect
    

提交回复
热议问题