Title Case is a Ruby script that parses a string and turns it into a title following the specifications loosely set by Daring Fireball's John Gruber in this article. After DF's post, Dan Benjamin at Hivelogic challenged the world to come up with a Ruby alternative to John's script, so here it is.
Logic
The script follows some basic rules, and has no hardcoded exceptions. There are two variables that the user is
encouraged (and expected) to change. They are "small_words" and "special_
characters". The small words variable
is self explanatory. The special characters variable contains characters that separate two words where you want
both of these words to become capitalized. For example, in the English language, one such character is a hyphen.
Words like "command-g" will become "Command-G" instead of just "Command-g". The first and last words of the
string are always capitalized, regardless of type, and words already containing capitals (iPhone, Mac-
World,
etc.) are untouched. Of course, no script can be perfect, so there will be always be a need for occasional
correction from a human being.
Usage
$ ruby ./title.rb "microsoft buys apple: hell freezes over"
=> "Microsoft Buys Apple: Hell Freezes Over"
Examples
| Daring Fireball Header | Downcased Header | Title-Cased Header |
|---|---|---|
| Microsoft’s Stiffy for ‘Advertising’ Now Extends to the Zune | microsoft’s stiffy for ‘advertising’ now extends to the zune | Microsoft’s Stiffy for ‘Advertising’ Now Extends to the Zune |
| Lyndon Lens | lyndon lens | Lyndon Lens |
| Microsoft Live Search Cashback | microsoft live search cashback | Microsoft Live Search Cashback |
| Twitter Downtime | twitter downtime | Twitter Downtime |
| Kubrick’s 50mm f/0.7 | kubrick’s 50mm f/0.7 | Kubrick’s 50mm F/0.7 |
| Roger Ebert: I Loved ‘Indy’ | roger ebert: i loved ‘indy’ | Roger Ebert: I Loved ‘Indy’ |
Get It
The script is available as a text file and also at github - the script is far from complete and there are bound
to be issues, so I'll be waiting for all the pull requests I'm sure will come flooding in. The text version for
download will be updated as often as possible. Lastly, I'd like to give a big thank-you to apeiros from
#ruby-lang on Freenode, who helped cut down on a lot of junk. Enjoy it everyone, and I hope this script shows
that Ruby is a viable competitor to Perl.
Go to Github or get the
text version now.