<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Ruby Way &#187; Data</title>
	<atom:link href="http://therubyway.org/category/data/feed/" rel="self" type="application/rss+xml" />
	<link>http://therubyway.org</link>
	<description>Unofficial Blog About &#34;The Ruby Way&#34; by Hal Fulton</description>
	<lastBuildDate>Mon, 05 Oct 2009 10:52:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Basic Ruby Idioms: Checking For An Empty String</title>
		<link>http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/</link>
		<comments>http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 17:35:41 +0000</pubDate>
		<dc:creator>Peter Cooper</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/</guid>
		<description><![CDATA[Page 15 of The Ruby Way (2nd edition) presents a basic Ruby idiom for checking whether a string is empty or not.
I believe there are three ways to do it (if you can think of any others, leave a comment!). Two reasonably obvious, but one that&#8217;s quite idiomatic (the one that The Ruby Way mentions):

str.empty? [...]]]></description>
			<content:encoded><![CDATA[<p>Page 15 of <i>The Ruby Way</i> (2nd edition) presents a basic Ruby idiom for checking whether a string is empty or not.</p>
<p>I believe there are three ways to do it (if you can think of any others, leave a comment!). Two reasonably obvious, but one that&#8217;s quite idiomatic (the one that <em>The Ruby Way</em> mentions):</p>
<ol>
<li><code>str.empty?</code> &#8211; With this we simply ask a String object if it&#8217;s &#8220;empty.&#8221;</li>
<li><code>str.length &gt; 0</code> &#8211; With this we check whether the string has a length of more than zero &#8211; i.e. it&#8217;s not empty.</li>
<li><code>str[0]</code> &#8211; With this we check the <i>first character</i> of the string. If no first character is present, both Ruby 1.8 and 1.9 return <code>nil</code>! This is particularly idiomatic and not something I&#8217;d considered before.</li>
</ol>
<p>Of course, the <i>shortest</i> or most idiomatic route is not usually the <i>best</i> way to go when programming, but if you&#8217;re playing code golf, doing some obfuscation, or otherwise want to access the first character of the string anyway, option #3 is undeniably sneaky.</p>
<blockquote>
<p>Note: Remember that while a string can only be empty or non-empty, a <i>variable</i> that you think is referring to a String object may well be <i>nil</i>! So often you need to check for <i>nil</i> as well as string length. You can do this with a simple <code>if str</code> or <code>if str.nil?</code></p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://therubyway.org/basic-ruby-idioms-checking-for-an-empty-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
