Ruby Language Create an Array of consecutive numbers or letters


Example

This can be easily accomplished by calling Enumerable#to_a on a Range object:

(1..10).to_a    #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

(a..b) means that it will include all numbers between a and b. To exclude the last number, use a...b

a_range = 1...5
a_range.to_a       #=> [1, 2, 3, 4]

or

('a'..'f').to_a    #=> ["a", "b", "c", "d", "e", "f"]
('a'...'f').to_a   #=> ["a", "b", "c", "d", "e"]

A convenient shortcut for creating an array is [*a..b]

[*1..10]           #=> [1,2,3,4,5,6,7,8,9,10]
[*'a'..'f']        #=> ["a", "b", "c", "d", "e", "f"]