[英]Generate array of all letters and digits
使用 ruby,是否可以轻松地制作字母表中每个字母和 0-9 的数组?
[*('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 ) ( Integer#to_s
方法将数字转换为以所需数字系统表示的字符串)
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
你也可以这样做:
'a'.upto('z').to_a + 0.upto(9).to_a
Try this:试试这个:
alphabet_array = [*'a'..'z', *'A'..'Z', *'0'..'9']
Or as string:或作为字符串:
alphabet_string = alphabet_array.join
你可以这样做:
("0".."Z").map { |i| i }
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"]
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.