Create an Array of consecutive numbers or letters

suggest change

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) includes every value from a through b. To exclude the last value, 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"]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents