Posted By

chrisaiv on 11/16/09


ruby String

Versions (?)

Ruby: Extend String Class to Capitalize Every Single Word

 / Published in: Ruby

Don't forget to use require 'directory/nameofclass'

  1. # This helper is opening up core Ruby String class
  2. # in order to add a new method to all Strings
  4. class String
  6. # Ruby has a capitalize method (used below) which capitalizes the
  7. # first letter of a string. But in order to capitalize the first
  8. # letter of EVERY word we have to write our own.
  10. def titleize
  11. self.split(' ').collect {|word| word.capitalize}.join(" ")
  12. end
  14. end

Report this snippet  

You need to login to post a comment.