<?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>John Leach's Blog &#187; when</title>
	<atom:link href="http://johnleach.co.uk/words/archives/tag/when/feed" rel="self" type="application/rss+xml" />
	<link>http://johnleach.co.uk/words</link>
	<description>Stuff I think, see and do</description>
	<lastBuildDate>Fri, 18 Jun 2010 22:57:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Ruby&#8217;s case statement uses ===</title>
		<link>http://johnleach.co.uk/words/archives/2009/08/30/402/rubys-case-statement-uses</link>
		<comments>http://johnleach.co.uk/words/archives/2009/08/30/402/rubys-case-statement-uses#comments</comments>
		<pubDate>Sun, 30 Aug 2009 19:48:16 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[case]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[condiitonal]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[switch]]></category>
		<category><![CDATA[when]]></category>

		<guid isPermaLink="false">http://johnleach.co.uk/words/?p=402</guid>
		<description><![CDATA[I&#8217;ve not found this stated clearly enough elsewhere so I&#8217;m doing so myself. Ruby&#8217;s case statement calls the === method on the argument to each of the when statements So, this example: case my_number when 6883 :prime end Will execute 6883 === my_number This is all fine and dandy, because the === method on a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve not found this stated clearly enough elsewhere so I&#8217;m doing so myself.</p>
<p><strong>Ruby&#8217;s case statement calls the <code>===</code> method on the argument to each of the when statements</strong></p>
<p>So, this example:</p>
<pre><code>case my_number
  when 6883
    :prime
end
</code></pre>
<p>Will execute <code>6883 === my_number</code></p>
<p>This is all fine and dandy, because the <code>===</code> method on a Fixnum instance does what you&#8217;d expect in this scenario.</p>
<p>However, the <code>===</code> method on the Fixnum <em>class</em> does something different.  It&#8217;s an alias of  <code>is_a?</code></p>
<p>That is cute, because it allows you to do this:</p>
<pre><code>case my_number
  when Fixnum
    "Easy to memorize"
  when Bignum
    "Hard to memorize"
  end
</code></pre>
<p>But it won&#8217;t work as you might expect in this scenario:</p>
<pre><code>my_type = Fixnum
case my_type
  when Fixnum
    "Fixed number"
end
</code></pre>
<p>This won&#8217;t work because <code>Fixnum === Fixnum</code> returns <code>false</code> because the <code>Fixnum</code> class is not an instance of <code>Fixnum</code>.</p>
<p>My workaround for this is to convert it to a string first. Not sure if that&#8217;s the best solution, but it works for me(tm).</p>
<pre><code>my_type = Fixnum
case my_type.to_s
  when "Fixnum"
    "Fixed number"
end
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://johnleach.co.uk/words/archives/2009/08/30/402/rubys-case-statement-uses/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
