Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Capitalizing titles with Ruby

This (mostly) follows the Microsoft Manual of Style for Technical Publications. Lame I know, but it's a reference:

   1  class String
   2     def titlecase
   3        non_capitalized = %w{of etc and by the for on is at to but nor or a via}
   4        gsub(/\b[a-z]+/){ |w| non_capitalized.include?(w) ? w : w.capitalize  }.sub(/^[a-z]/){|l| l.upcase }.sub(/\b[a-z][^\s]*?$/){|l| l.capitalize }
   5     end
   6  end


Examples:

   1  "this is a story in the new york times".titleize # => "This is a Story In the New York Times"
   2  "what in the world was that for?".titleize # => "What In the World Was That For?"
   3  "searching for a CHEAP overhead projector?".titleize # => "Searching for a CHEAP Overhead Projector?"
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS