<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: strings code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 12:13:34 GMT</pubDate>
    <description>DZone Snippets: strings code</description>
    <item>
      <title>Capitalizing titles in Ruby - another take...</title>
      <link>http://snippets.dzone.com/posts/show/557</link>
      <description>This code was inspired by Peter's code from May 17... &lt;br /&gt;&lt;br /&gt;I basically extended his idea to be more flexible and added ! methods to modify the string in place. I chose to use a different regex selector because using "\b" breaks on apostrophes and leave you with words like "You'Re" instead of "You're"...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class String&lt;br /&gt;&lt;br /&gt;  def titlecase()&lt;br /&gt;    ignore_list = %w{of etc and by the for on is at to but nor or a via}&lt;br /&gt;    capitalize_all_ex(ignore_list)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def titlecase!()&lt;br /&gt;    ignore_list = %w{of etc and by the for on is at to but nor or a via}&lt;br /&gt;    capitalize_all_ex!(ignore_list)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all(force_downcase = true)&lt;br /&gt;    ignore_list = %w{}&lt;br /&gt;    capitalize_all_ex(ignore_list, force_downcase)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all!(force_downcase = true)&lt;br /&gt;    ignore_list = %w{}&lt;br /&gt;    capitalize_all_ex!(ignore_list, force_downcase)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all_ex(ignore_list, force_downcase = true)&lt;br /&gt;    # if force_downcase is true then the &lt;br /&gt;    # string is, um, downcased first :-)&lt;br /&gt;    if force_downcase&lt;br /&gt;      self.downcase.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      }&lt;br /&gt;    else&lt;br /&gt;      self.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      }&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all_ex!(ignore_list, force_downcase = true)&lt;br /&gt;    if force_downcase&lt;br /&gt;      self.replace(self.downcase.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      })&lt;br /&gt;    else&lt;br /&gt;      self.replace(self.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      })&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 06 Aug 2005 00:09:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/557</guid>
      <author>s0nspark (Tim Ferrell)</author>
    </item>
  </channel>
</rss>
