Creating an array of all letters and numbers

Using ruby, is it possible to easily create an array of each letter in the alphabet and 0-9?

+84
ruby
Jan 31 '11 at 1:19
source share
7 answers
[*('a'..'z'), *('0'..'9')] # doesn't work in Ruby 1.8 

or

 ('a'..'z').to_a + ('0'..'9').to_a # works in 1.8 and 1.9 

or

 (0...36).map{ |i| i.to_s 36} 
+130
Jan 31 '11 at 1:26
source share

for letters or numbers, you can create ranges and iterate over them. try this to get a general idea:

 ("a".."z").each { |letter| p letter } 

to get an array from it just try the following:

 ("a".."z").to_a 
+32
Jan 31 '11 at 1:24
source share

You can also do it like this:

 'a'.upto('z').to_a + 0.upto(9).to_a 
+8
Jun 11 '14 at 18:33
source share

Try this:

 alphabet_array = [*'a'..'z', *'A'..'Z', *'0'..'9'] 

Or as a string:

 alphabet_string = alphabet_array.join 
+4
Mar 01 '18 at 19:58
source share
 myarr = [*?a..?z] #generates an array of strings for each letter a to z myarr = [*?a..?z] + [*?0..?9] # array of strings az and 0-9 
+2
Feb 24 '16 at 1:36
source share
 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"]

+2
Apr 7 '19 at 19:45
source share

You can simply do this:

 ("0".."Z").map { |i| i } 
+1
Oct. 25 '16 at 19:08
source share



All Articles