Skip to content

Printing the alphabet easily in Ruby on Rails

Posted on 04 May 2006

It's really easy to print out the alphabet with Ruby. I needed to do recently as an additional method for searching by letter. Although, I could see it might be useful for pagination.

The code I used is as follows:

<ul>
  <% "A".upto("Z") do |l| -%>	    
     <li><%= link_to l, genre_letter_url(:letter => l) %></li>	
  <% end %>
</ul>

The key bit is "A".upto("Z") do |l|. Note that if you use lowercase letters then your output will be in lowercase.

If you are wondering about my use of genre_letter_url(:letter => l) This is best explained in my article about routes in ruby on rails and in particular named routes.

View the snippets archive ››

1 comment made

na43251 commented on 01 Oct 07 at 22:02

i like better


('A'..'Z').each do |l| ...

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com