110

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

Phrogz
  • 296,393
  • 112
  • 651
  • 745
JP Silvashy
  • 46,977
  • 48
  • 149
  • 227

7 Answers7

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
  • 2
    Is 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
  • 4
    please 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
  • 3
    Benchmarking 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
  • 1
    Using 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
12

You can also do it this way:

'a'.upto('z').to_a + 0.upto(9).to_a
user3731366
  • 133
  • 1
  • 5
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
3

You can just do this:

("0".."Z").map { |i| i }
giordanofalves
  • 173
  • 2
  • 8
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