Generate array of all letters and digits

后端 未结 7 920
温柔的废话
温柔的废话 2021-01-30 07:49

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

相关标签:
7条回答
  • 2021-01-30 08:23

    Try this:

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

    Or as string:

    alphabet_string = alphabet_array.join
    
    0 讨论(0)
  • 2021-01-30 08:27

    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
    
    0 讨论(0)
  • 2021-01-30 08:29
    [*('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 }
    

    (the Integer#to_s method converts a number to a string representing it in a desired numeral system)

    0 讨论(0)
  • 2021-01-30 08:34

    You can just do this:

    ("0".."Z").map { |i| i }
    
    0 讨论(0)
  • 2021-01-30 08:35

    You can also do it this way:

    'a'.upto('z').to_a + 0.upto(9).to_a
    
    0 讨论(0)
  • 2021-01-30 08:38
    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"]

    0 讨论(0)
提交回复
热议问题