Ruby Language Arrays Create an Array of consecutive numbers or letters

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


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]


('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"]

Got any Ruby Language Question?