<?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>Virtuous Code &#187; sbpp</title>
	<atom:link href="http://devblog.avdi.org/tag/sbpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://devblog.avdi.org</link>
	<description>&#34;The three virtues of a programmer: laziness, impatience, and hubris&#34; -- Larry Wall</description>
	<lastBuildDate>Fri, 17 May 2013 15:24:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Another Take on Smalltalk-Style Controll Flow (SBPP #6)</title>
		<link>http://devblog.avdi.org/2011/12/19/another-take-on-smalltalk-style-controll-flow-sbpp-6/</link>
		<comments>http://devblog.avdi.org/2011/12/19/another-take-on-smalltalk-style-controll-flow-sbpp-6/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 14:00:00 +0000</pubDate>
		<dc:creator>Avdi Grimm</dc:creator>
				<category><![CDATA[Smalltalk Best Practice Patterns]]></category>
		<category><![CDATA[flow-control]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sbpp]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://avdi.org/devblog/?p=1935</guid>
		<description><![CDATA[The latest Smalltalk-to-Ruby translation in my SBPPRB archive is &#8220;Dispatched Interpretation&#8221;. It&#8217;s one of the bigger ones I&#8217;ve tackled so far. I&#8217;m not going to go over the whole pattern here; for that you&#8217;ll just need to buy a copy &#8230; <a href="http://devblog.avdi.org/2011/12/19/another-take-on-smalltalk-style-controll-flow-sbpp-6/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The <a href="https://github.com/avdi/sbpprb/blob/master/20_dispatched_interpretation.rb">latest Smalltalk-to-Ruby translation in my SBPPRB archive is &#8220;Dispatched Interpretation&#8221;</a>. It&#8217;s one of the bigger ones I&#8217;ve tackled so far. I&#8217;m not going to go over the whole pattern here; for that you&#8217;ll just need to buy a copy of <a href="http://www.amazon.com/gp/product/013476904X/ref=as_li_ss_tl?ie=UTF8&amp;tag=thlafa-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=013476904X">Smalltalk Best Practice Patterns</a> :-) </p>
<p> However, one potentially interesting snippet is this one: </p>
<pre class="src src-ruby"><span class="org-keyword">class</span> <span class="org-type">Object</span>
  <span class="org-keyword">def</span> <span class="org-function-name">if_false</span>
    <span class="org-variable-name">self</span>
  <span class="org-keyword">end</span>
<span class="org-keyword">end</span>

<span class="org-keyword">class</span> <span class="org-type">TrueClass</span>
  <span class="org-keyword">def</span> <span class="org-function-name">if_true</span>
    <span class="org-keyword">yield</span>
  <span class="org-keyword">end</span>
<span class="org-keyword">end</span>

<span class="org-keyword">class</span> <span class="org-type">FalseClass</span>
  <span class="org-keyword">def</span> <span class="org-function-name">if_false</span>
    <span class="org-keyword">yield</span>
  <span class="org-keyword">end</span>

  <span class="org-keyword">def</span> <span class="org-function-name">if_true</span>
    <span class="org-variable-name">self</span>
  <span class="org-keyword">end</span>
<span class="org-keyword">end</span>
</pre>
<p> This is another take on Smalltalk-style method-based control flow. Specifically, the following Smalltalk idiom: </p>
<pre class="example">aPredicate ifTrue: [ ... ] ifFalse: [ ... ]
</pre>
<p> The key difference here from <a href="https://github.com/avdi/sbpprb/blob/master/flow_control.rb">our last foray into Smalltalk-style flow control</a> is a special implementation of <code>#if_true</code> on <code>FalseClass</code>, which ignores the given block and instead returns itself. This enables a call to <code>#if_false</code> to be chained onto the <code>#if_true</code> call, and the <code>#if_false</code> defined on <code>FalseClass</code> then yields to its block in order to execute the negative case. </p>
<p> On the flip side, if the initial object is <code>true</code>, then <code>TrueClass</code>&#8216; <code>#if_true</code> method is hit, which yields to the given block. It returns the result value of the block, which, assuming it is not <code>false</code>, will respond to <code>#if_false</code> with the do-nothing method we defined on <code>Object</code>. There is an obvious bug in the case where the block given to <code>if_true</code> does, in fact, return the <code>false</code> constant. I&#8217;ll leave fixing that bug as an exercise to the reader. </p>
<p> Bugs aside, the upshot is a chained call idiom which looks not unlike the Smalltalk keyword argument version: </p>
<pre class="src src-ruby">object.if_true{ puts <span class="org-string">"true!"</span>}.if_false{ puts <span class="org-string">"false!"</span>}
</pre>
<p> As with some of the other Smalltalk-in-Ruby articles, I put this forward as a curiosity. If you really want to use method-based flow control pervasively, Ruby might not be the best language for it. </p>
[ERROR: No "bytitle", "bypath" or "byid" arguments where provided with the boilerplate shortcode or the values provided did not match an existing boilerplate entry.]
]]></content:encoded>
			<wfw:commentRss>http://devblog.avdi.org/2011/12/19/another-take-on-smalltalk-style-controll-flow-sbpp-6/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
