subscribe to RSS

A Title Case Gem for Ruby

Posted Monday, February 02, 2009  

A project I’m working on called for some “smart” capitalisation of page titles. Essentially I wanted to take a URL slug) and generate a page title.

Rails comes with a built-in String#titleize method that capitalises every word but that looked a little odd when the title was something like: “My Hovercraft Is Full Of Eels”. So I went on a hunt for something “smarter”.

After a little search I stumbled upon Marshall Elfstrand’s JavaScript, Ruby, and Objective-C ports of John Gruber’s “Title Case” algorithm and decided to turn it into a Gem that adds String#titleize and String#titleize! (aliased as #titlecase, and #titlecase! respectively). When used in a Rails environment, this effectively replaces the Rails versions.

Now my page titles look a little more human-like: “My Hovercraft is Full of Eels”.

Tweet This Delicious Reddit Stumble Upon Digg Share on Tumblr email

About Simon

Husband, Father, One-time Entrepreneur.

Aka Haruki Zaemon. Aka Sampy.

In my younger years I wanted to save the world; now I'm happy solving bigger problems than I create.

If I didn't need to work I'd be teaching Aikido and spending all my free time with my amazing wife and two children in Woodend, Victoria, Australia.

Books

Beginning Algorithms
with James Ross

Software

Simian
Similarity Analyser

Blog Categories