Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

666 lines (496 sloc) 61.921 kb
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>2 Kestrels</title>
<link href="stylesheet.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="leanpub-main">
<h2 section-num="2" id="kestrels"><span class="section-number">2 </span>Kestrels</h2>
<p>In Combinatory Logic, a Kestrel (or &#x201C;K Combinator&#x201D;) is a function that returns a constant function, normally written <code>Kxy = x</code>. In Ruby, it might look like this:</p>
<div class="code-block">
<div class="highlight"><pre><code class="c"># for *any* x,</code>
<code class="n">kestrel</code><code class="p">.</code><code class="n">call</code><code class="p">(:</code><code class="n">foo</code><code class="p">).</code><code class="n">call</code><code class="p">(</code><code class="n">x</code><code class="p">)</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">:</code><code class="n">foo</code>
</pre></div>
</div>
<p>Kestrels are to be found in Ruby. You may be familiar with their Ruby 1.9 name, <code>#tap</code>. Let&#x2019;s say you have a line like <code>address = Person.find(...).address</code> and you wish to log the person instance. With <code>tap</code>, you can inject some logging into the expression without messy temporary variables:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">address</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="nb">find</code><code class="p">(...).</code><code class="n">tap</code> <code class="p">{</code> <code class="o">|</code><code class="n">p</code><code class="o">|</code> <code class="n">logger</code><code class="p">.</code><code class="nb">log</code> "<code class="n">person</code> #<code class="p">{</code><code class="n">p</code><code class="p">}</code> <code class="n">found</code>" <code class="p">}.</code><code class="n">addre</code><code class="o">\</code>
<code class="n">ss</code>
</pre></div>
</div>
<p><code>tap</code> is a method in all objects that passes <code>self</code> to a block and returns self, ignoring whatever the last item of the block happens to be. Ruby on Rails programmers will recognize the Kestrel in slightly different form:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">address</code> <code class="p">=</code> <code class="n">returning</code> <code class="n">Person</code><code class="p">.</code><code class="nb">find</code><code class="p">(...)</code> <code class="n">do</code> <code class="o">|</code><code class="n">p</code><code class="o">|</code>
<code class="n">logger</code><code class="p">.</code><code class="nb">log</code> "<code class="n">person</code> #<code class="p">{</code><code class="n">p</code><code class="p">}</code> <code class="n">found</code>"
<code class="k">end</code><code class="p">.</code><code class="n">address</code>
</pre></div>
</div>
<p>Again, the result of the block is discarded, it is only there for side effects. This behaviour is the same as a Kestrel. Remember <code>kestrel.call(:foo).call(x)</code>? If I rewrite it like this, you can see the similarity:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">Kestrel</code><code class="p">.</code><code class="n">call</code><code class="p">(:</code><code class="n">foo</code><code class="p">)</code> <code class="n">do</code>
<code class="n">x</code>
<code class="k">end</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">:</code><code class="n">foo</code>
</pre></div>
</div>
<p>Both <code>returning</code> and <code>tap</code> are handy for grouping side effects together. Methods that look like this:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code>
<code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="n">person</code>
<code class="k">end</code>
</pre></div>
</div>
<p>Can be rewritten using <code>returning</code>:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">returning</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code> <code class="n">do</code> <code class="o">|</code><code class="n">person</code><code class="o">|</code>
<code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>It is obvious from the first line what will be returned and it eliminates an annoying error when the programmer neglects to make <code>person</code> the last line of the method.</p>
<h3 section-num="2.1" id="object-initializer-blocks"><span class="section-number">2.1 </span>Object initializer blocks</h3>
<p>The Kestrel has also been sighted in the form of <em>object initializer blocks</em>. Consider this example using <a href="http://blog.grayproductions.net/articles/all_about_struct" title="All about Struct">Struct</a>:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">Contact</code> <code class="p">=</code> <code class="n">Struct</code><code class="p">.</code><code class="n">new</code><code class="p">(:</code><code class="n">first</code><code class="p">,</code> <code class="p">:</code><code class="n">last</code><code class="p">,</code> <code class="p">:</code><code class="n">email</code><code class="p">)</code> <code class="n">do</code>
<code class="n">def</code> <code class="n">to_hash</code>
<code class="n">Hash</code><code class="p">[</code><code class="o">*</code><code class="n">members</code><code class="p">.</code><code class="n">zip</code><code class="p">(</code><code class="n">values</code><code class="p">).</code><code class="n">flatten</code><code class="p">]</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>The method <code>Struct#new</code> creates a new class. It also accepts an optional block, evaluating the block for side effects only. It returns the new class regardless of what happens to be in the block (it happens to evaluate the block in class scope, a small refinement).</p>
<p>You can use this technique when writing your own classes:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">class</code> <code class="n">Bird</code> <code class="o">&lt;</code> <code class="n">Creature</code>
<code class="n">def</code> <code class="n">initialize</code><code class="p">(</code><code class="o">*</code><code class="n">params</code><code class="p">)</code>
# <code class="n">do</code> <code class="n">something</code> <code class="n">with</code> <code class="n">the</code> <code class="n">params</code>
<code class="n">yield</code> <code class="n">self</code> <code class="k">if</code> <code class="n">block_given</code>?
<code class="k">end</code>
<code class="k">end</code>
<code class="n">Forest</code><code class="p">.</code><code class="n">add</code><code class="p">(</code>
<code class="n">Bird</code><code class="p">.</code><code class="n">new</code><code class="p">(:</code><code class="n">name</code> <code class="p">=</code><code class="o">&gt;</code> <code class="s">'</code><code class="err">Kestrel) { |k| combinators &lt;&lt; k }</code>
<code class="p">)</code>
</pre></div>
</div>
<p>The pattern of wanting a Kestrel/returning/tap when you create a new object is so common that building it into object initialization is useful. And in fact, it&#x2019;s built into <code>ActiveRecord</code>. Methods like <code>new</code> and <code>create</code> take optional blocks, so you can write:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">class</code> <code class="n">Person</code> <code class="o">&lt;</code> <code class="n">ActiveRecord</code><code class="p">::</code><code class="n">Base</code>
# <code class="p">...</code>
<code class="k">end</code>
<code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code> <code class="n">do</code> <code class="o">|</code><code class="n">person</code><code class="o">|</code>
<code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>In Rails, <code>returning</code> is not necessary when creating instances of your model classes, thanks to ActiveRecord&#x2019;s built-in object initializer blocks.</p>
<h3 section-num="2.2" id="inside-an-idiomatic-ruby-kestrel"><span class="section-number">2.2 </span>Inside, an idiomatic Ruby Kestrel</h3>
<p>When we discussed <code>Struct</code> above, we noted that its initializer block has a slightly different behaviour than <code>tap</code> or <code>returning</code>. It takes an initializer block, but it doesn&#x2019;t pass the new class to the block as a parameter, it evaluates the block in the context of the new class.</p>
<p>Putting this into implementation terms, it evaluates the block with <code>self</code> set to the new class. This is not the same as <code>returning</code> or <code>tap</code>, both of which leave <code>self</code> untouched. We can write our own version of <code>returning</code> with the same semantics. We will call it <code>inside</code>:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">module</code> <code class="n">Kernel</code>
<code class="n">def</code> <code class="n">inside</code><code class="p">(</code><code class="n">value</code><code class="p">,</code> <code class="o">&amp;</code><code class="n">block</code><code class="p">)</code>
<code class="n">value</code><code class="p">.</code><code class="n">instance_eval</code><code class="p">(</code><code class="o">&amp;</code><code class="n">block</code><code class="p">)</code>
<code class="n">value</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>You can use this variation on a Kestrel just like <code>returning</code>, only you do not need to specify a parameter:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">inside</code> <code class="p">[</code>1<code class="p">,</code> 2<code class="p">,</code> 3<code class="p">]</code> <code class="n">do</code>
<code class="n">uniq</code>!
<code class="k">end</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">[</code>1<code class="p">,</code> 2<code class="p">,</code> 3<code class="p">]</code>
</pre></div>
</div>
<p>This isn&#x2019;t particularly noteworthy. Of more interest is your access to private methods and instance variables:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">sna</code> <code class="p">=</code> <code class="n">Struct</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="s">'Fubar'</code><code class="p">)</code> <code class="n">do</code>
<code class="n">attr_reader</code> <code class="p">:</code><code class="n">fu</code>
<code class="k">end</code><code class="p">.</code><code class="n">new</code>
<code class="n">inside</code><code class="p">(</code><code class="n">sna</code><code class="p">)</code> <code class="n">do</code>
<code class="p">@</code><code class="n">fu</code> <code class="p">=</code> <code class="s">'bar'</code>
<code class="k">end</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">struct</code> <code class="n">Struct</code><code class="p">::</code><code class="n">Fubar</code> <code class="o">&gt;</code>
<code class="n">sna</code><code class="p">.</code><code class="n">fu</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="s">'bar'</code>
</pre></div>
</div>
<p><code>inside</code> is a Kestrel just like <code>returning</code>. No matter what value its block generates, it returns its primary argument. The only difference between the two is the evaluation environment of the block.</p>
<h3 section-num="2.3" id="the-enchaining-kestrel"><span class="section-number">2.3 </span>The Enchaining Kestrel</h3>
<p>In <strong>Kestrels</strong>, we looked at <code>#tap</code> from Ruby 1.9 and <code>returning</code> from Ruby on Rails. No we&#x2019;ll going to look at another use for <code>tap</code>. As already explained, Ruby 1.9 includes the new method <code>Object#tap</code>. It passes the receiver to a block, then returns the receiver no matter what the block contains. The canonical example inserts some logging in the middle of a chain of method invocations:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">address</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="nb">find</code><code class="p">(...).</code><code class="n">tap</code> <code class="p">{</code> <code class="o">|</code><code class="n">p</code><code class="o">|</code> <code class="n">logger</code><code class="p">.</code><code class="nb">log</code> "<code class="n">person</code> #<code class="p">{</code><code class="n">p</code><code class="p">}</code> <code class="n">found</code>" <code class="p">}.</code><code class="n">addre</code><code class="o">\</code>
<code class="n">ss</code>
</pre></div>
</div>
<p><code>Object#tap</code> is also useful when you want to execute several method on the same object without having to create a lot of temporary variables, a practice Martin Fowler calls [Method Chaining](http://martinfowler.com/dslwip/MethodChaining.html &#x201C;&#x201D;). Typically, you design such an object so that it returns itself in response to every modifier message. This allows you to write things like:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">HardDrive</code><code class="p">.</code><code class="n">new</code><code class="p">.</code><code class="n">capacity</code><code class="p">(</code>150<code class="p">).</code><code class="n">external</code><code class="p">.</code><code class="n">speed</code><code class="p">(</code>7200<code class="p">)</code>
</pre></div>
</div>
<p>Instead of:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">hd</code> <code class="p">=</code> <code class="n">HardDrive</code><code class="p">.</code><code class="n">new</code>
<code class="n">hd</code><code class="p">.</code><code class="n">capacity</code> <code class="p">=</code> 150
<code class="n">hd</code><code class="p">.</code><code class="n">external</code> <code class="p">=</code> <code class="n">true</code>
<code class="n">hd</code><code class="p">.</code><code class="n">speed</code> <code class="p">=</code> 7200
</pre></div>
</div>
<p>And if you are a real fan of the Kestrel, you would design your class with an object initializer block so you could write:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">hd</code> <code class="p">=</code> <code class="n">HardDrive</code><code class="p">.</code><code class="n">new</code> <code class="n">do</code>
<code class="p">@</code><code class="n">capacity</code> <code class="p">=</code> 150
<code class="p">@</code><code class="n">external</code> <code class="p">=</code> <code class="n">true</code>
<code class="p">@</code><code class="n">speed</code> <code class="p">=</code> 7200
<code class="k">end</code>
</pre></div>
</div>
<p>But what do you do when handed a class that was not designed with method chaining in mind? For example, <code>Array#pop</code> returns the object being popped, not the array. Before you validate every criticism levelled against Ruby for allowing programmers to rewrite methods in core classes, consider using <code>#tap</code> with <code>Symbol#to_proc</code> or <code>String#to_proc</code> to chain methods without rewriting them.</p>
<p>So instead of</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">fizz</code><code class="p">(</code><code class="n">arr</code><code class="p">)</code>
<code class="n">arr</code><code class="p">.</code><code class="n">pop</code>
<code class="n">arr</code><code class="p">.</code><code class="n">map</code>! <code class="p">{</code> <code class="o">|</code><code class="n">n</code><code class="o">|</code> <code class="n">n</code> <code class="o">*</code> 2 <code class="p">}</code>
<code class="k">end</code>
</pre></div>
</div>
<p>We can write:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">fizz</code><code class="p">(</code><code class="n">arr</code><code class="p">)</code>
<code class="n">arr</code><code class="p">.</code><code class="n">tap</code><code class="p">(</code><code class="o">&amp;</code><code class="p">:</code><code class="n">pop</code><code class="p">).</code><code class="n">map</code>! <code class="p">{</code> <code class="o">|</code><code class="n">n</code><code class="o">|</code> <code class="n">n</code> <code class="o">*</code> 2 <code class="p">}</code>
<code class="k">end</code>
</pre></div>
</div>
<p>I often use <code>#tap</code> to enchain methods for those pesky array methods that sometimes do what you expect and sometimes don&#x2019;t. My most hated example is <a href="http://ruby-doc.org/core/classes/Array.html#M002238" title="Class: Array"><code>Array#uniq!</code></a>:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">arr</code> <code class="p">=</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
<code class="n">arr</code><code class="p">.</code><code class="n">uniq</code><code class="p">,</code> <code class="n">arr</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">],</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
<code class="n">arr</code> <code class="p">=</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
<code class="n">arr</code><code class="p">.</code><code class="n">uniq</code>!<code class="p">,</code> <code class="n">arr</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">],</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
<code class="n">arr</code> <code class="p">=</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
<code class="n">arr</code><code class="p">.</code><code class="n">uniq</code><code class="p">,</code> <code class="n">arr</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">],</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
<code class="n">arr</code> <code class="p">=</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
<code class="n">arr</code><code class="p">.</code><code class="n">uniq</code>!<code class="p">,</code> <code class="n">arr</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="n">nil</code><code class="p">,</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
</pre></div>
</div>
<p>Let&#x2019;s replay that last one in slow motion:</p>
<div class="code-block">
<div class="highlight"><pre><code class="p">[</code> 1<code class="p">,</code> 2<code class="p">,</code> 3<code class="p">,</code> 4<code class="p">,</code> 5 <code class="p">].</code><code class="n">uniq</code>!
<code class="p">=</code><code class="o">&gt;</code> <code class="n">nil</code>
</pre></div>
</div>
<p>That might be a problem. For example:</p>
<div class="code-block">
<div class="highlight"><pre><code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">].</code><code class="n">uniq</code>!<code class="p">.</code><code class="n">sort</code>!
<code class="p">=</code><code class="o">&gt;</code> <code class="n">NoMethodError</code><code class="p">:</code> <code class="n">undefined</code> <code class="n">method</code> `<code class="n">sort</code>!<code class="s">'</code><code class="err"> for nil:NilClass</code>
</pre></div>
</div>
<p><code>Object#tap</code> to the rescue: When using a method like <code>#uniq!</code> that modifies the array in place and sometimes returns the modified array but sometimes helpfully returns <code>nil</code>, I can use <code>#tap</code> to make sure I always get the array, which allows me to enchain methods:</p>
<div class="code-block">
<div class="highlight"><pre><code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">].</code><code class="n">tap</code><code class="p">(</code><code class="o">&amp;</code><code class="p">:</code><code class="n">uniq</code>!<code class="p">).</code><code class="n">sort</code>!
<code class="p">=</code><code class="o">&gt;</code> <code class="p">[</code>1<code class="p">,</code>2<code class="p">,</code>3<code class="p">,</code>4<code class="p">,</code>5<code class="p">]</code>
</pre></div>
</div>
<p>So there&#x2019;s another use for <code>#tap</code> (along with <code>Symbol#to_proc</code> for simple cases): We can use it when we want to enchain methods, but the methods do not return the receiver.</p>
<blockquote>
<p>In Ruby 1.9, <code>#tap</code> works exactly as described above. Ruby 1.8 does not have <code>#tap</code>, but you can obtain it by installing the andand gem. This version of <code>#tap</code> also works like a Quirky Bird, so you can write things like <code>HardDrive.new.tap.capacity(150)</code> for enchaining methods that take parameters and/or blocks. To get andand, <code>sudo gem install andand</code>. Rails users can also drop <code>andand.rb</code> in <code>config/initializers</code>.</p>
</blockquote>
<h3 section-num="2.4" id="the-obdurate-kestrel"><span class="section-number">2.4 </span>The Obdurate Kestrel</h3>
<p>The <a href="http://github.com/raganwald/andand/tree" title="raganwald's andand">andand gem</a> includes <code>Object#tap</code> for Ruby 1.8. It also includes another kestrel called <code>#dont</code>. Which does what it says, or rather <em>doesn&#x2019;t</em> do what it says.</p>
<div class="code-block">
<div class="highlight"><pre><code class="p">:</code><code class="n">foo</code><code class="p">.</code><code class="n">tap</code> <code class="p">{</code> <code class="n">p</code> <code class="s">'bar'</code> <code class="p">}</code>
<code class="n">bar</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">:</code><code class="n">foo</code> # <code class="n">printed</code> <code class="s">'bar'</code> <code class="n">before</code> <code class="n">returning</code> <code class="n">a</code> <code class="n">value</code>!
<code class="p">:</code><code class="n">foo</code><code class="p">.</code><code class="n">dont</code> <code class="p">{</code> <code class="n">p</code> <code class="s">'bar'</code> <code class="p">}</code>
<code class="p">=</code><code class="o">&gt;</code> <code class="p">:</code><code class="n">foo</code> # <code class="n">without</code> <code class="n">printing</code> <code class="s">'bar'</code>!
</pre></div>
</div>
<p><code>Object#dont</code> simply ignores the block passed to it. So what is it good for? Well, remember our logging example for <code>#tap</code>?</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">address</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="nb">find</code><code class="p">(...).</code><code class="n">tap</code> <code class="p">{</code> <code class="o">|</code><code class="n">p</code><code class="o">|</code> <code class="n">logger</code><code class="p">.</code><code class="nb">log</code> "<code class="n">person</code> #<code class="p">{</code><code class="n">p</code><code class="p">}</code> <code class="n">found</code>" <code class="p">}.</code><code class="n">addre</code><code class="o">\</code>
<code class="n">ss</code>
</pre></div>
</div>
<p>Let&#x2019;s turn the logging off for a moment:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">address</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="nb">find</code><code class="p">(...).</code><code class="n">dont</code> <code class="p">{</code> <code class="o">|</code><code class="n">p</code><code class="o">|</code> <code class="n">logger</code><code class="p">.</code><code class="nb">log</code> "<code class="n">person</code> #<code class="p">{</code><code class="n">p</code><code class="p">}</code> <code class="n">found</code>" <code class="p">}.</code><code class="n">addr</code><code class="o">\</code>
<code class="n">ess</code>
</pre></div>
</div>
<p>And back on:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">address</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="nb">find</code><code class="p">(...).</code><code class="n">tap</code> <code class="p">{</code> <code class="o">|</code><code class="n">p</code><code class="o">|</code> <code class="n">logger</code><code class="p">.</code><code class="nb">log</code> "<code class="n">person</code> #<code class="p">{</code><code class="n">p</code><code class="p">}</code> <code class="n">found</code>" <code class="p">}.</code><code class="n">addre</code><code class="o">\</code>
<code class="n">ss</code>
</pre></div>
</div>
<p>I typically use it when doing certain kinds of primitive debugging. And it has another trick up its sleeve:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">arr</code><code class="p">.</code><code class="n">dont</code><code class="p">.</code><code class="n">sort</code>!
</pre></div>
</div>
<p>Look at that, it works with method calls like a quirky bird! So you can use it to <code>NOOP</code> methods. Now, you could have done that with <code>Symbol#to_proc</code>:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">arr</code><code class="p">.</code><code class="n">dont</code><code class="p">(</code><code class="o">&amp;</code><code class="p">:</code><code class="n">sort</code>!<code class="p">)</code>
</pre></div>
</div>
<p>But what about methods that take parameters and blocks?</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">JoinBetweenTwoModels</code><code class="p">.</code><code class="n">dont</code><code class="p">.</code><code class="n">create</code>!<code class="p">(...)</code> <code class="n">do</code> <code class="o">|</code><code class="n">new_join</code><code class="o">|</code>
# <code class="p">...</code>
<code class="k">end</code>
</pre></div>
</div>
<p><code>Object#dont</code> is the Ruby-semantic equivalent of commenting out a method call, only it can be inserted inside of an existing expression. That&#x2019;s why it&#x2019;s called the <em>obdurate kestrel</em>. It refuses to do anything!</p>
<p>If you want to try <code>Object#dont</code>, or want to use <code>Object#tap</code> with Ruby 1.8, <code>sudo gem install andand</code>. Rails users can also drop <code>andand.rb</code> in <code>config/initializers</code> as mentioned above. Enjoy!</p>
<h3 section-num="2.5" id="kestrels-on-rails"><span class="section-number">2.5 </span>Kestrels on Rails</h3>
<p>As mentioned, Ruby on Rails provides #returning, a method with K Combinator semantics:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">returning</code><code class="p">(</code><code class="n">expression</code><code class="p">)</code> <code class="n">do</code> <code class="o">|</code><code class="n">name</code><code class="o">|</code>
# <code class="n">name</code> <code class="n">is</code> <code class="n">bound</code> <code class="n">to</code> <code class="n">the</code> <code class="n">result</code> <code class="n">of</code> <code class="n">evaluating</code> <code class="n">expression</code>
# <code class="n">this</code> <code class="n">block</code> <code class="n">is</code> <code class="n">evaluated</code> <code class="n">and</code> <code class="n">the</code> <code class="n">result</code> <code class="n">is</code> <code class="n">discarded</code>
<code class="k">end</code>
<code class="p">=</code><code class="o">&gt;</code> # <code class="n">the</code> <code class="n">result</code> <code class="n">of</code> <code class="n">evaluating</code> <code class="n">the</code> <code class="n">expression</code> <code class="n">is</code> <code class="n">now</code> <code class="n">returned</code>
</pre></div>
</div>
<p>Rails also provides <em>object initializer blocks</em> for ActiveRecord models. Here&#x2019;s an example from one of my unit tests:</p>
<div class="code-block">
<div class="highlight"><pre><code class="p">@</code><code class="n">board</code> <code class="p">=</code> <code class="n">Board</code><code class="p">.</code><code class="n">create</code><code class="p">(:</code><code class="n">dimension</code> <code class="p">=</code><code class="o">&gt;</code> 9<code class="p">)</code> <code class="n">do</code> <code class="o">|</code><code class="n">b</code><code class="o">|</code>
<code class="n">b</code><code class="p">[</code><code class="s">'aa'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'bb'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'cb'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'da'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'ba'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'white'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'ca'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'white'</code>
<code class="k">end</code>
</pre></div>
</div>
<p>So, it looks like in Rails you can choose between an object initializer block and #returning:</p>
<div class="code-block">
<div class="highlight"><pre><code class="p">@</code><code class="n">board</code> <code class="p">=</code> <code class="n">returning</code><code class="p">(</code><code class="n">Board</code><code class="p">.</code><code class="n">create</code><code class="p">(:</code><code class="n">dimension</code> <code class="p">=</code><code class="o">&gt;</code> 9<code class="p">))</code> <code class="n">do</code> <code class="o">|</code><code class="n">b</code><code class="o">|</code>
<code class="n">b</code><code class="p">[</code><code class="s">'aa'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'bb'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'cb'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'da'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'black'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'ba'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'white'</code>
<code class="n">b</code><code class="p">[</code><code class="s">'ca'</code><code class="p">]</code> <code class="p">=</code> <code class="s">'white'</code>
<code class="k">end</code>
</pre></div>
</div>
<p>In both cases the created object is returned regardless of what the block would otherwise return. But beyond that, the two Kestrels have very different semantics. &#x201C;Returning&#x201D; fully evaluates the expression, in this case creating the model instance in its entirety, including all of its callbacks. The object initializer block, on the other hand, is called as part of initializing the object <em>before</em> starting the lifecycle of the object including its callbacks.</p>
<p>&#x201C;Returning&#x201D; is what you want when you want to do stuff involving the fully created object and you are trying to logically group the other statements with the creation. In my case, that&#x2019;s what I want, I am trying to say that @board is a board with black stones on certain intersections and white stones on other intersections.</p>
<p>Object initialization is what you want when you want to initialize certain fields by hand and perform some calculations or logic before kicking off the object creation lifecycle. That wasn&#x2019;t what I wanted in this case because my <code>[]=</code> method depended on the object being initialized. So my code had a bug that was fixed when I changed from object initializers to #returning.</p>
<p>Summary: In Rails, object initializers are evaluated before the object&#x2019;s life cycle is started, #returning&#x2019;s block is evaluated afterwards. And that is today&#x2019;s <em>lingua obscura</em>.</p>
<h3 section-num="2.6" id="rewriting-returning-in-rails"><span class="section-number">2.6 </span>Rewriting &#x201C;Returning&#x201D; in Rails</h3>
<p>One of the most useful tools provided by Ruby on Rails is the #returning method, a simple but very useful implementation of the K Combinator or Kestrel. For example, this:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code>
<code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="n">person</code>
<code class="k">end</code>
</pre></div>
</div>
<p>Can and should be expressed using #returning as this:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">returning</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code> <code class="n">do</code> <code class="o">|</code><code class="n">person</code><code class="o">|</code>
<code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>Why? Firstly, you avoid the common bug of forgetting to return the object you are creating:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">broken_registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code>
<code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">end</code>
</pre></div>
</div>
<p>This creates the person object and does the initialization you want, but doesn&#x2019;t actually return it from the method, it returns whatever #send_email_notification happens to return. If you&#x2019;ve worked hard to create fluent interfaces you might be correct by accident, but #send_email_notification could just as easily return the email it creates. Who knows?</p>
<p>Second, in methods like this as you read from top to bottom you are declaring what the method returns right up front:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">returning</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code> <code class="n">do</code> # <code class="p">...</code>
# <code class="p">...</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>It takes some optional params and returns a new person. Very clear. And the third reason I like #returning is that it logically clusters the related statements together:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">returning</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code> <code class="n">do</code> <code class="o">|</code><code class="n">person</code><code class="o">|</code>
<code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">end</code>
</pre></div>
</div>
<p>It is very clear that these statements are all part of one logical block. As a bonus, my IDE respects that and it&#x2019;s easy to fold them or drag them around as a single unit. All in all, I think #returning is a big win and I even look for opportunities to refactor existing code to use it whenever I&#x2019;m making changes.</p>
<p>
<strong>DWIM</strong>
</p>
<p>All that being said, I have observed a certain bug or misapplication of #returning from time to time. It&#x2019;s usually pretty subtle in production code, but I&#x2019;ll make it obvious with a trivial example. What does this snippet evaluate to?</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">returning</code> <code class="p">[</code>1<code class="p">]</code> <code class="n">do</code> <code class="o">|</code><code class="n">numbers</code><code class="o">|</code>
<code class="n">numbers</code> <code class="o">&lt;&lt;</code> 2
<code class="n">numbers</code> <code class="o">+</code><code class="p">=</code> <code class="p">[</code>3<code class="p">]</code>
<code class="k">end</code>
</pre></div>
</div>
<p>This is the kind of thing that sadistic interviewers use in coding quizzes. The answer is <strong>[1, 2]</strong>, not [1, 2, 3]. The <code>&lt;&lt;</code> operator mutates the value assigned to the numbers variable, but the <code>+=</code> statement overwrites the reference assigned to the numbers variable without changing the original value. #returning remembers the <em>value</em> originally assigned to numbers and returns it. If you have some side-effects on that value, those count. But assignment does nothing to the value.</p>
<p>This may seem obvious, but in my experience it is a subtle point that causes difficulty. Languages with referential transparency escape the confusion entirely, but OO languages like Ruby have this weird thing where we have to keep track of references and labels on references in our head.</p>
<p>Here&#x2019;s something contrived to look a lot more like production code. First, without #returning:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">working_registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code>
<code class="k">if</code> <code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">else</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(:</code><code class="n">default</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">)</code>
<code class="k">end</code>
<code class="n">person</code>
<code class="k">end</code>
</pre></div>
</div>
<p>And here we&#x2019;ve refactored it to use #returning:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">broken_registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">returning</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code> <code class="n">do</code> <code class="o">|</code><code class="n">person</code><code class="o">|</code>
<code class="k">if</code> <code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">else</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(:</code><code class="n">default</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">)</code>
<code class="k">end</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>Oops! This no longer works as we intended. Overwriting the <code>person</code> variable is irrelevant, #returning returns the unregistered new person no matter what. So what&#x2019;s going on here?</p>
<p>One answer is to &#x201C;blame the victim.&#x201D; Ruby has a certain well-documented behaviour around variables and references. #returning has a certain well-documented behaviour. Any programmer who makes the above mistake is&#x2013;well&#x2013;mistaken. Fix the code and set the bug ticket status to Problem Between Keyboard And Chair (&#x201C;PBKAC&#x201D;).</p>
<p>Another answer is to suggest that the implementation of #returning is at fault. If you write:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">returning</code> <code class="p">...</code> <code class="n">do</code> <code class="o">|</code><code class="n">var</code><code class="o">|</code>
# <code class="p">...</code>
<code class="n">var</code> <code class="p">=</code> <code class="n">something_else</code>
# <code class="p">...</code>
<code class="k">end</code>
</pre></div>
</div>
<p>You intended to change what you are returning from #returning. So #returning should be changed to do what you meant. I&#x2019;m on the fence about this. When folks argue that designs should cater to programmers who do not understand the ramifactions of the programming language or of the framework, I usually retort that you cannot have progress and innovation while clinging to familiarity, <a href="http://weblog.raganwald.com/2008/01/programming-language-cannot-be-better.html" title="A programming language cannot be better without being unintuitive">an argument I first heard from Jef Raskin</a>. The real meaning of &#x201C;The Principle of Least Surprise&#x201D; is that a design should be <em>internally consistent</em>, which is not the same thing as <em>familiar</em>.</p>
<p>Ruby&#x2019;s existing use of variables and references is certainly consistent. And once you know what #returning does, it remains consistent. However, this design decision isn&#x2019;t really about being consistent with Ruby&#x2019;s implementation, we are debating how an idiom should be designed. I think we have a blank canvas and it&#x2019;s reasonable to at least <em>consider</em> a version of #returning that handles assignment to the parameter.</p>
<p>
<strong>Rewriting #returning</strong>
</p>
<p>The <a href="http://github.com/raganwald/rewrite_rails/tree/master">RewriteRails</a> plug-in adds syntactic abstractions like <a href="http://github.com/raganwald/rewrite_rails/tree/master/doc/andand.textile" title="&quot;) and [String to Block](http://github.com/raganwald/rewrite_rails/tree/master/doc/string_to_block.md#readme &quot;">Andand</a> to Rails projects <a href="http://avdi.org/devblog/2008/02/23/why-monkeypatching-is-destroying-ruby/" title="Monkeypatching is Destroying Ruby">without monkey-patching</a>. RewriteRails now includes its own version of #returning that overrides the #returning shipping with Rails.</p>
<p>When RewriteRails is processing source code, it turns code like this:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">returning</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">))</code> <code class="n">do</code> <code class="o">|</code><code class="n">person</code><code class="o">|</code>
<code class="k">if</code> <code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">else</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(:</code><code class="n">default</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">)</code>
<code class="k">end</code>
<code class="k">end</code>
<code class="k">end</code>
</pre></div>
</div>
<p>Into this:</p>
<div class="code-block">
<div class="highlight"><pre><code class="n">def</code> <code class="n">registered_person</code><code class="p">(</code><code class="n">params</code> <code class="p">=</code> <code class="p">{})</code>
<code class="n">lambda</code> <code class="n">do</code> <code class="o">|</code><code class="n">person</code><code class="o">|</code>
<code class="k">if</code> <code class="n">Registry</code><code class="p">.</code><code class="n">register</code><code class="p">(</code><code class="n">person</code><code class="p">)</code>
<code class="n">person</code><code class="p">.</code><code class="n">send_email_notification</code>
<code class="k">else</code>
<code class="n">person</code> <code class="p">=</code> <code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(:</code><code class="n">default</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">)</code>
<code class="k">end</code>
<code class="n">person</code>
<code class="k">end</code><code class="p">.</code><code class="n">call</code><code class="p">(</code><code class="n">Person</code><code class="p">.</code><code class="n">new</code><code class="p">(</code><code class="n">params</code><code class="p">.</code><code class="n">merge</code><code class="p">(:</code><code class="n">registered</code> <code class="p">=</code><code class="o">&gt;</code> <code class="n">true</code><code class="p">)))</code>
<code class="k">end</code>
</pre></div>
</div>
<p>Note that in addition to turning the #returning &#x201C;call&#x201D; into a lambda that is invoked immediately, it also makes sure the new lambda returns the <code>person</code> variable&#x2019;s contents. So assignment to the variable does change what #returning appears to return.</p>
<p>Like all processors in RewriteRails, #returning is only rewritten in <code>.rr</code> files that you write in your project. Existing <code>.rb</code> files are not affected, including all code in the Rails framework: RewriteRails will never monkey with other people&#x2019;s expectations. RewriteRails doesn&#x2019;t physically modify the .rr files you write: The rewritten code is put in another file that the Ruby interpreter sees. So you see the code you write and RewriteRails figures out what to show the interpreter. This is a little like a Lisp macro. </p>
</div>
<div id="leanpub-toc">
<h2>Table of Contents</h2>
<ol class="toc">
<li class="section"><a href="chap00.html#the-mit-license"><span class="section-number">0.1 </span>The MIT License</a></li>
<li class="section"><a href="chap00.html#preface"><span class="section-number">0.2 </span>Preface</a></li>
<li class="chapter"><a href="chap01.html#introduction"><span class="section-number">1 </span>Introduction</a></li>
<li class="chapter"><a href="chap02.html#kestrels"><span class="section-number">2 </span>Kestrels</a></li>
<li class="section"><a href="chap02.html#object-initializer-blocks"><span class="section-number">2.1 </span>Object initializer blocks</a></li>
<li class="section"><a href="chap02.html#inside-an-idiomatic-ruby-kestrel"><span class="section-number">2.2 </span>Inside, an idiomatic Ruby Kestrel</a></li>
<li class="section"><a href="chap02.html#the-enchaining-kestrel"><span class="section-number">2.3 </span>The Enchaining Kestrel</a></li>
<li class="section"><a href="chap02.html#the-obdurate-kestrel"><span class="section-number">2.4 </span>The Obdurate Kestrel</a></li>
<li class="section"><a href="chap02.html#kestrels-on-rails"><span class="section-number">2.5 </span>Kestrels on Rails</a></li>
<li class="section"><a href="chap02.html#rewriting-returning-in-rails"><span class="section-number">2.6 </span>Rewriting &#x201C;Returning&#x201D; in Rails</a></li>
<li class="chapter"><a href="chap03.html#the-thrush"><span class="section-number">3 </span>The Thrush</a></li>
<li class="section"><a href="chap03.html#let"><span class="section-number">3.1 </span>Let</a></li>
<li class="chapter"><a href="chap04.html#songs-of-the-cardinal"><span class="section-number">4 </span>Songs of the Cardinal</a></li>
<li class="section"><a href="chap04.html#building-a-cardinal-in-ruby"><span class="section-number">4.1 </span>Building a Cardinal in Ruby</a></li>
<li class="chapter"><a href="chap05.html#quirky-birds-and-meta-syntactic-programming"><span class="section-number">5 </span>Quirky Birds and Meta-Syntactic Programming</a></li>
<li class="section"><a href="chap05.html#a-limited-interpretation-of-the-quirky-bird-in-ruby"><span class="section-number">5.1 </span>A limited interpretation of the Quirky Bird in Ruby</a></li>
<li class="section"><a href="chap05.html#embracing-the-quirky-bird"><span class="section-number">5.2 </span>Embracing the Quirky Bird</a></li>
<li class="section"><a href="chap05.html#andand-even-more"><span class="section-number">5.3 </span>Andand even more</a></li>
<li class="chapter"><a href="chap06.html#aspect-oriented-programming-in-ruby-using-combinator-birds"><span class="section-number">6 </span>Aspect-Oriented Programming in Ruby using Combinator Birds</a></li>
<li class="section"><a href="chap06.html#giving-methods-advice"><span class="section-number">6.1 </span>Giving methods advice</a></li>
<li class="section"><a href="chap06.html#the-super-keyword-perhaps-youve-heard-of-it"><span class="section-number">6.2 </span>The super keyword, perhaps you&#x2019;ve heard of it?</a></li>
<li class="section"><a href="chap06.html#the-queer-bird"><span class="section-number">6.3 </span>The Queer Bird</a></li>
<li class="chapter"><a href="chap07.html#mockingbirds"><span class="section-number">7 </span>Mockingbirds</a></li>
<li class="section"><a href="chap07.html#duplicative-combinators"><span class="section-number">7.1 </span>Duplicative Combinators</a></li>
<li class="section"><a href="chap07.html#recursive-lambdas-in-ruby"><span class="section-number">7.2 </span>Recursive Lambdas in Ruby</a></li>
<li class="section"><a href="chap07.html#recursive-combinatorics"><span class="section-number">7.3 </span>Recursive Combinatorics</a></li>
<li class="section"><a href="chap07.html#recursive-combinators-in-idiomatic-ruby"><span class="section-number">7.4 </span>Recursive Combinators in Idiomatic Ruby</a></li>
<li class="section"><a href="chap07.html#the-mockingbird"><span class="section-number">7.5 </span>The Mockingbird</a></li>
<li class="chapter"><a href="chap08.html#refactoring-methods-with-recursive-combinators"><span class="section-number">8 </span>Refactoring Methods with Recursive Combinators</a></li>
<li class="section"><a href="chap08.html#divide-and-conquer"><span class="section-number">8.1 </span>Divide and Conquer</a></li>
<li class="section"><a href="chap08.html#the-merge-sort"><span class="section-number">8.2 </span>The Merge Sort</a></li>
<li class="section"><a href="chap08.html#separating-declaration-from-implementation"><span class="section-number">8.3 </span>Separating Declaration from Implementation</a></li>
<li class="section"><a href="chap08.html#practical-recursive-combinators"><span class="section-number">8.4 </span>Practical Recursive Combinators</a></li>
<li class="section"><a href="chap08.html#spicing-things-up"><span class="section-number">8.5 </span>Spicing things up</a></li>
<li class="section"><a href="chap08.html#building-on-a-legacy"><span class="section-number">8.6 </span>Building on a legacy</a></li>
<li class="section"><a href="chap08.html#seriously"><span class="section-number">8.7 </span>Seriously</a></li>
<li class="section"><a href="chap08.html#separating-implementation-from-declaration"><span class="section-number">8.8 </span>Separating Implementation from Declaration</a></li>
<li class="section"><a href="chap08.html#a-really-simple-recursive-combinator"><span class="section-number">8.9 </span>A Really Simple Recursive Combinator</a></li>
<li class="chapter"><a href="chap09.html#you-cant-be-serious"><span class="section-number">9 </span>You can&#x2019;t be serious!?</a></li>
<li class="section"><a href="chap09.html#string-to-proc"><span class="section-number">9.1 </span>String to Proc</a></li>
<li class="section"><a href="chap09.html#the-message"><span class="section-number">9.2 </span>The Message</a></li>
<li class="chapter"><a href="chap10.html#the-hopelessly-egocentric-book-chapter"><span class="section-number">10 </span>The Hopelessly Egocentric Book Chapter</a></li>
<li class="section"><a href="chap10.html#object-oriented-egocentricity"><span class="section-number">10.1 </span>Object-oriented egocentricity</a></li>
<li class="chapter"><a href="chap11.html#bonus-chapter-separating-concerns-in-coffeescript-using-aspect-oriented-programming"><span class="section-number">11 </span>Bonus Chapter: Separating Concerns in Coffeescript using Aspect-Oriented Programming</a></li>
<li class="chapter"><a href="chap12.html#appendix-finding-joy-in-combinators"><span class="section-number">12 </span>Appendix: Finding Joy in Combinators</a></li>
<li class="section"><a href="chap12.html#languages-for-combinatorial-logic"><span class="section-number">12.1 </span>Languages for combinatorial logic</a></li>
<li class="section"><a href="chap12.html#concatenative-languages"><span class="section-number">12.2 </span>Concatenative languages</a></li>
<li class="chapter"><a href="chap13.html#appendix-source-code"><span class="section-number">13 </span>Appendix: Source Code</a></li>
<li class="section"><a href="chap13.html#kestrels-1"><span class="section-number">13.1 </span>kestrels</a></li>
<li class="section"><a href="chap13.html#thrushes"><span class="section-number">13.2 </span>thrushes</a></li>
<li class="section"><a href="chap13.html#the-cardinal"><span class="section-number">13.3 </span>the cardinal</a></li>
<li class="section"><a href="chap13.html#quirky-birds"><span class="section-number">13.4 </span>quirky birds</a></li>
<li class="section"><a href="chap13.html#bluebirds"><span class="section-number">13.5 </span>bluebirds</a></li>
<li class="chapter"><a href="chap14.html#about-the-author"><span class="section-number">14 </span>About The Author</a></li>
<li class="section"><a href="chap14.html#contact"><span class="section-number">14.1 </span>contact</a></li>
</ol>
</div>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.