<?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>Artisan Coder &#187; ruby</title>
	<atom:link href="http://www.artisancoder.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artisancoder.com</link>
	<description>Software development as a craft</description>
	<lastBuildDate>Mon, 23 Aug 2010 13:19:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Equivalent of and-let*?</title>
		<link>http://www.artisancoder.com/2008/12/equivalent-of-and-let/</link>
		<comments>http://www.artisancoder.com/2008/12/equivalent-of-and-let/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 17:33:47 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[srfi-2]]></category>

		<guid isPermaLink="false">http://www.ventonegro.org/?p=64</guid>
		<description><![CDATA[By this point I believe it&#8217;s clear that I am a big fan of Scheme. Lately I have seen a sharp rise of interest in Ruby in some virtual places I attend (#lisp-br, twitter, IM with coworkers etc.). I am sure Ruby is a fine language, even in spite of several complaints I hear from [...]]]></description>
			<content:encoded><![CDATA[<p>By this point I believe it&#8217;s clear that I am a big fan of <a href="http://www.schemers.org/" title="An improper list of Scheme resources">Scheme</a>. Lately I have seen a sharp rise of interest in Ruby in some virtual places I attend (#lisp-br, twitter, IM with coworkers etc.). I am sure Ruby is a fine language, even in spite of several complaints I hear from some of its users. After all, it has symbols, metaprogramming, blocks (a sort of closures), <em>lambdas</em> and more! I&#8217;d really take the time to learn it, if it wasn&#8217;t for <a href="http://www.paulgraham.com/quotes.html" title="Lisp Quotes">Matz himself</a>:</p>
<blockquote>
<p>Some may say Ruby is a bad rip-off of Lisp or Smalltalk, and I admit that. But it is nicer to ordinary people.</p>
</blockquote>
<p>Now <em>that</em> is a demotivator. But Ruby must be evolving, and that quote may be outdated. So I have thought of a question to the Ruby-lovers, that may help me make up my mind to take the time to learn it. One of the best Scheme macros is, in my opinion, <tt>and-let*</tt>, from the <a href="http://srfi.schemers.org/srfi-2/srfi-2.html" title="AND-LET*: an AND with local bindings, a guarded LET* special form">SRFI-2</a>. How would one write the following in Ruby, a procedure that I wrote a couple of nights ago?</p>

<div class="wp_codebox"><table><tr id="p642"><td class="code" id="p64code2"><pre class="scheme" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Tries to get session from current request</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>get<span style="color: #66cc66;">-</span>session request<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>and<span style="color: #66cc66;">-</span><span style="color: #b1b100;">let*</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cookies <span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">-</span>cookies request<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>p <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> <span style="color: #ff0000;">&quot;session_id&quot;</span> cookies<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>sid<span style="color: #66cc66;">-</span>str <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>sid <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">-&gt;</span>number sid<span style="color: #66cc66;">-</span>str<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">integer?</span> sid<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">exact?</span> sid<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>sp <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assq</span> sid <span style="color: #66cc66;">*</span>sessions<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> sp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>In the <tt>and-let*</tt> macro, the clauses are evaluated and the variables are bound sequentially, creating new scopes for the next expressions. If any of the forms evaluates to <tt>#f</tt> (false in Scheme), the computation is aborted, no other expressions are evaluated, and the whole form evaluates to <tt>#f</tt>. How would I write this in concise Ruby?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artisancoder.com/2008/12/equivalent-of-and-let/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
