<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Bjh5537's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 22:54:26 GMT</pubDate>
    <description>DZone Snippets: Bjh5537's Code Snippets</description>
    <item>
      <title>Default Values for Blank Values OO-style</title>
      <link>http://snippets.dzone.com/posts/show/5289</link>
      <description>In Ruby the idiom || can be used to provide a default value if something is nil.  This can't be done with blank since an empty string is not nil and will short-circuit the || operator, leaving most of us to to do something like "name.blank? ? 'John Doe' : name".  Here's a way to do this OO-style:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Object  &lt;br /&gt;  def or_if_blank(value)  &lt;br /&gt;    self.respond_to?(:blank) &amp;&amp; self.blank? ? value : self  &lt;br /&gt;  end  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;name.or_if_blank("John Doe")&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 29 Mar 2008 01:59:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5289</guid>
      <author>bjh5537 (Rails Garden)</author>
    </item>
  </channel>
</rss>
