Using ruby, is it possible to make an array of each letter in the alphabet and 0-9 easily?
Asked
Active
Viewed 8.5k times
7 Answers
169
[*('a'..'z'), *('0'..'9')] # doesn't work in Ruby 1.8
or
('a'..'z').to_a + ('0'..'9').to_a
or
(0...36).map{ |i| i.to_s 36 }
(the Integer#to_s
method converts a number to a string representing it in a desired numeral system)

Nakilon
- 34,866
- 14
- 107
- 142
-
2Is there a particular name for the `*` operator used in this context? It's new to me. – Michael Burr Jan 31 '11 at 01:38
-
1@Michael Burr, *splat operator*. See [here](http://stackoverflow.com/questions/918449/what-is-the-operator-doing-to-this-string-in-ruby) and [here](http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/) – Nakilon Jan 31 '11 at 01:40
-
4please indicate in answer that the * code sample will work in Ruby 1.9 but not in Ruby 1.8 – Zabba Jan 31 '11 at 01:46
-
2@Zabba, you just indicated it in your comment ..) – Nakilon Jan 31 '11 at 01:56
-
3Benchmarking under Ruby 2.1 [*('a'..'z'),*('0'..'9')] is a little more than twice as fast as (0...36).map{|i|i.to_s 36} (1.450000s versus 2.26000s where n = 100,000). If inclusion of upcase is desired, use the following: [*('a'..'z'),*('A'..'Z'),*('0'..'9')] – Viet Feb 20 '14 at 20:27
-
1Using the Base36 number range is **genius**! Exactly what I was looking for. I created a directory structure for file distribution. All file names are base 36 numbers. Each file is then stored in its associated directory (e.g., filename[0]). – Clint Pachl Sep 12 '14 at 21:10
-
@Nakilon could you please explain how the 0...36 work what are these numbers? i know about the three points but as a beginner, I don't know about the 0 to 36 – StudentAccount4 May 10 '20 at 15:58
-
@Nakilon want to create array containing [a,b,c,d.....] till 'n'. if limit exceed 26, then it should increse like [a,b,c......z,aa,ab and so on] – Anshul Riyal May 14 '21 at 23:17
-
@AnshulRiyal try this: `> ("x"..).take(5)` => `["x", "y", "z", "aa", "ab"]` -- the "endless ranges" is a new feature in 2.7 or something. – Nakilon May 26 '21 at 15:05
39
for letters or numbers you can form ranges and iterate over them. try this to get a general idea:
("a".."z").each { |letter| p letter }
to get an array out of it, just try the following:
("a".."z").to_a

Pete
- 17,885
- 4
- 32
- 30
11
Try this:
alphabet_array = [*'a'..'z', *'A'..'Z', *'0'..'9']
Or as string:
alphabet_string = alphabet_array.join

kwyntes
- 1,045
- 10
- 27
3
myarr = [*?a..?z] #generates an array of strings for each letter a to z
myarr = [*?a..?z] + [*?0..?9] # array of strings a-z and 0-9

user1290366
- 31
- 3
3
letters = *('a'..'z')
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Daniel
- 153
- 2
- 12
-
Even though this may answer the question, there is no explanation of your code. Please update your answer to provide an explanation of what you are doing. Thanks! – Miroslav Glamuzina Apr 07 '19 at 20:17