Given I have an integer value of, e.g., 10.
10
How can I create an array of 10 elements like [1,2,3,4,5,6,7,8,9,10]?
[1,2,3,4,5,6,7,8,9,10]
You can just splat a range:
[*1..10] #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Ruby 1.9 allows multiple splats, which is rather handy:
[*1..3, *?a..?c] #=> [1, 2, 3, "a", "b", "c"]
2.1m questions
2.1m answers
60 comments
57.0k users