Generate array of all letters and digits

Ruby

Ruby Problem Overview


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

Ruby Solutions


Solution 1 - Ruby

[*('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)

Solution 2 - Ruby

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

Solution 3 - Ruby

You can also do it this way:

'a'.upto('z').to_a + 0.upto(9).to_a

Solution 4 - Ruby

Try this:

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

Or as string:

alphabet_string = alphabet_array.join

Solution 5 - Ruby

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

Solution 6 - Ruby

You can just do this:

("0".."Z").map { |i| i }

Solution 7 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJP SilvashyView Question on Stackoverflow
Solution 1 - RubyNakilonView Answer on Stackoverflow
Solution 2 - RubyPeteView Answer on Stackoverflow
Solution 3 - Rubyuser3731366View Answer on Stackoverflow
Solution 4 - RubykwyntesView Answer on Stackoverflow
Solution 5 - Rubyuser1290366View Answer on Stackoverflow
Solution 6 - RubygiordanofalvesView Answer on Stackoverflow
Solution 7 - RubyDanielView Answer on Stackoverflow