Alphanumeric list in Ruby

Creating a list of A-Z and 0-9 is very simple in Ruby. You may need to do this for numerous reasons but the writing of this tip came about when needing to create a memorable word interface, like that of banks.

Ruby has great list capabilities, for example listing 0-9 is as simple as:

1
2
3
0..9.each do |n| 
  puts n
end

Doing so with the alphabet is just as easy:

1
2
3
('A'..'Z').to_a.each do |l|
  puts l
end

To create an array of both A-Z and 0-9 together the following can be used:

1
list = ('A'...'Z').to_a + (0..9).to_a

It’s that simple!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>