<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Category ruby_for_nubys</title>
    <link>http://talklikeaduck.denhaven2.com/articles/category/ruby_for_nubys</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>You Can't Judge a Book...  Some Mental Traps in Learning Ruby</title>
      <description>&lt;div style='width:160; float:left; text-align:right; font-size:xx-small; border-width:1px; border-color:#444444; margin-bottom:30px; margin-right:30px;border-style:solid;'class="tease-image"&gt;
&lt;img width='160' height='240' alt='Bo Diddley, Hoyer' src='http://farm1.static.flickr.com/180/379366968_c8876333ee_m.jpg'
&gt;
&lt;br/&gt;
&lt;a href='http://flickr.com/photos/hubmedia/379366968/'&gt;Bo Diddley, Hoyer&lt;/a&gt;
&lt;br/&gt;&amp;copy;
&lt;a href='http://flickr.com/people/hubmedia'&gt;Andy Field&lt;/a&gt;
&lt;br/&gt;&lt;a href='http://creativecommons.org/licenses/by-nc-sa/2.0/'&gt;&lt;img src='http://i.creativecommons.org/l/by-nc-sa/2.0/80x15.png' title='used under a Creative Commons Attribution-NonCommercial-ShareAlike License' width='80' height='15' border='0'/&gt;&lt;/a&gt;
&lt;/div&gt;
 Quick,  in the Ruby expression:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="ident"&gt;a&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;10&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 
&lt;p&gt;What's the class of a?&lt;/p&gt;
&lt;p&gt;Many beginning, and intermediate, rubyists whould instinctively say Array!  The real answer is that given just this code, you don't know.&lt;/p&gt;
&lt;p&gt;Recently, on the rails-talk mailing list there was a discussion of this &lt;a href="http://weblog.jamisbuck.org/2007/4/4/activerecord-base-find-shortcut"&gt;idea from Jamis Buck.&lt;/a&gt; Jamis uses alias_method in ActiveRecord::Base so that he can use shortcuts like:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="constant"&gt;Person&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;as a shortcut for:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="constant"&gt;Person&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is particularly nice when playing with ActiveRecord in the rails console, but many seem to think that this should be made part of Rails core, and I don't see a real objection.&lt;/p&gt;
&lt;p&gt;Now one objection raised by the folks who answer Array to the opening question is that, since ActiveRecord rows can be deleted, Person[2] might fail even though Person[1], and Person[3] work.  The reasoning is that the [] makes it "look like an array" and arrays don't act like that.&lt;/p&gt; 
&lt;p&gt;To me this is a case of judging a book by looking at it's cover. Something which I strongly suggest needs to be resisted in order to achieve ruby mastery.&lt;/p&gt;
&lt;p&gt;Let's have a look at a couple of "can't see past the cover" traps in Ruby.&lt;/p&gt;



&lt;h2&gt;Trap Number 1: Confusing Calling Interface with Class&lt;/h2&gt;
&lt;p&gt;Seeing a[2] and thinking Array is an example of this. Just because 99% of the time when you see something "indexed" by an integer, that thing doesn't have to be an Array. It might be an Array, but it &lt;stress&gt;could&lt;/stress&gt; be a Hash, or an ActiveRecord class, or anything which implemented some form of [] method.  This is looking at the method invocation as a 'cover.'&lt;/p&gt;
&lt;p&gt;Ruby method signatures are quite a bit more flexible or at least different from those of other languages, something which can trip up someone trying to see Ruby from the perspective of their 'native' language.  Consider the varieties of parameters which can be passed to Array#[], or String#[], rather than just a indexing methods these can slice and dice the receiver in various ways&lt;/p&gt;
&lt;p&gt;Although different classes might all implement a [] method, and while there are likely similarities or common subsets of functionality, each has it's own quirks.  This can drive folks expecting type signatures nuts, but it's actually one of the things which gives Ruby it's power, and efficiency of expression.&lt;/p&gt;
&lt;p&gt;Some folks go to great efforts trying to pin-down Ruby and make it 'behave' like other languages, but that's a fools errand which works against Rubys strengths.  I'd recommend that these folks redirect their efforts towards improving their testing skills.&lt;/p&gt;
&lt;h2&gt;Trap Number 2: Ignoring the Contents&lt;/h2&gt;
&lt;p&gt;For better or worse, many Ruby classes provide functions which may not work for all instances.  For example, the Range (1..10) is enumerable while (1.0..10.0) is not.  This is because some of the methods in range expect the beginning and ending values of the range to implement methods such as #succ in order to determine the next value.  Further they implicitly expect that applying #succ successively will get to the end value.&lt;/p&gt;
&lt;p&gt;So does this mean that making the range (1.0..10.0) is ilicit or bad?  Not if you don't need to enumerate it and only want to do things like check whether or not a value is included in the range.&lt;/p&gt;
&lt;p&gt;Some folks on ruby-talk have expressed the opinion that the documentation for Range implies that (1.0-10.0) is not a valid range, because the scope of the requirement for elements to implement #succ is ambiguous.  Pragmatically I find the ability to construct enumeration challenged ranges useful.&lt;/p&gt;
&lt;p&gt;Further if you take the philosophy which leads to restricting ranges to elements which make the ranges enumerable consider these things:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="punct"&gt;%w(&lt;/span&gt;&lt;span class="string"&gt;Now is the time&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;sort&lt;/span&gt;      &lt;span class="comment"&gt;# Works fine&lt;/span&gt;
&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:fee&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:fie&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:foe&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:fum&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;sort&lt;/span&gt; &lt;span class="comment"&gt;# Fails &lt;/span&gt;
&lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;fred&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="number"&gt;42&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;sort&lt;/span&gt;             &lt;span class="comment"&gt;# Fails&lt;/span&gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now do we really want to outlaw arrays of symbols, or heterogeneous arrays from Ruby usage because they don't sort?&lt;/p&gt;
&lt;p&gt;What do you think?&lt;/p&gt;</description>
      <pubDate>Wed, 17 Oct 2007 13:01:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:b835c6d9-8f12-4bbc-a344-96c2e6053c62</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>best_practices</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/472</trackback:ping>
    </item>
    <item>
      <title>Ahh, the Subtleties of Ruby's Operator Precedence</title>
      <description>&lt;p&gt;&lt;img src="http://talklikeaduck.denhaven2.com/files/precedence.png" class="tease-image"/&gt;
&lt;p&gt;Not long ago while working on an existing Rails application, (i.e. code I hadn&amp;#8217;t written). I was mystified 
when a logical expression seemed to be returning an odd result.  The expression had been written with &lt;strong&gt;not&lt;/strong&gt;,
&lt;strong&gt;and&lt;/strong&gt;, and &lt;strong&gt;or&lt;/strong&gt;, and was the right hand side of an assignment statement.  ventually I changed to using &lt;strong&gt;!&lt;/strong&gt;,
&lt;strong&gt;&amp;#38;&amp;#38;&lt;/strong&gt;, and &lt;strong&gt;||&lt;/strong&gt; which fixed the problem. 
I never completely
understood what was going on, until I encountered this 
&lt;a href="http://blog.jayfields.com/2007/08/ruby-operator-precedence-of-and-which.html"&gt;blog entry by Jay Fields.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the big differences between Ruby and Smalltalk is the use of operator precedence.  In Smalltalk, the grammar is
very simple, unary message selectors bind tighter than binary selectors, which bind tighter than keyword selectors.
The assignment operator binds more loosely than any of these.  This is often a stumbling block for newcomers to Smalltalk
when they find that in Smalltalk 3 + 5 * 2 evaluates to 16.&lt;/p&gt;
&lt;p&gt;Ruby in comparison has quite a rich and flexible syntax.
Usually, it&amp;#8217;s quite intutitive, but, depending on &lt;a href="http://talklikeaduck.denhaven2.com/articles/2007/06/21/where-i-come-from"&gt;where you come from&lt;/a&gt; there can be some surprises.&lt;/p&gt;
&lt;p&gt;I understood the relative precendences of, say &lt;strong&gt;&amp;#38;&amp;#38;&lt;/strong&gt; vs. &lt;strong&gt;and&lt;/strong&gt; in Ruby, but the fact that 
&lt;strong&gt;and&lt;/strong&gt; had weaker precedence than &lt;strong&gt;=&lt;/strong&gt; escaped me, until Jay&amp;#8217;s article turned the light on for
me.&lt;/p&gt;
&lt;p&gt;As they say, you learn something new everyday, or at least you hope you do.  I don&amp;#8217;t expect that I&amp;#8217;ll forget this little
corner of Ruby in future&lt;/p&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 07 Aug 2007 15:24:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0f8801a7-807d-4bfb-9686-ef64b0eb3a05</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/08/07/ahh-the-subtleties-of-rubys-operator-precedence</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>gotchas</category>
      <category>syntax</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/452</trackback:ping>
    </item>
    <item>
      <title>Polishing That Chain</title>
      <description>A few days back I wrote this:
&lt;blockquote&gt;
The metaclass&amp;#8217; superclass is set to the
metaclass of the classes superclass.&lt;/blockquote&gt;
&lt;p&gt;&lt;a name="#clarification"&gt;&lt;/a&gt;
&lt;p&gt;Since I first wrote it I&amp;#8217;ve gotten some feedback that the last sentence is a little hard to digest, so I&amp;#8217;ve added an example. 
&lt;blockquote&gt;Let&amp;#8217;s say we create a class MyNiftyClass as a subclass of Object.  The singleton class of the class object referenced by the MyNiftyClass global gets created at the same time as class object, no waiting for one of the things which trigger the creation of a singleton class for a non-class object.  Thesuperclass of the MyNiftyClass is set to object, and the superclass of MyNiftyClass&amp;#8217; metaclass is set to Object&amp;#8217;s metaclass.&lt;/p&gt;
&lt;p&gt;Besides the fact that their creation occurs immediately, there is one other difference  between
singleton classes of objects and singleton classes used as
metaclasses. The later can have subclasses, while the former cannot.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I&amp;#8217;ve gone back and editted &lt;a href]"http://talklikeaduck.denhaven2.com/articles/2007/06/02/chain-chain-chain"&gt;the original post&lt;/a&gt;, but I&amp;#8217;ve summarized it here in case anyone else has actually already read it.&lt;/p&gt;</description>
      <pubDate>Tue, 12 Jun 2007 17:22:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:e76ffc99-4ae0-4079-aa07-199403d64e75</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/12/polishing-that-chain</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Update on Continuations</title>
      <description>Robert Dober discovered that the continuation example which I gave in &lt;a href="http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-continuations"&gt;last night&amp;#8217;s article&lt;/a&gt; behaves badly if you run it with ruby rather than irb.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve made note of this in the original article, and I&amp;#8217;ve added a simpler example which does work under ruby. I&amp;#8217;ll try to come back with a new article which shows an example of how continuations are normally used.&lt;/p&gt;</description>
      <pubDate>Wed, 06 Jun 2007 17:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d75926cc-6f91-41e4-a722-633ed35ed5d5</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/06/update-on-continuations</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>closures</category>
      <category>continuations</category>
    </item>
    <item>
      <title>Closing in on Closures and Jumping into Continuations</title>
      <description>Recently on ruby-talk someone asked if continuations and closures were the same thing.
&lt;p&gt;They are related in that they both are tied to a particular point in time in the execution of a program.  The difference is that closures are like a souvenir of a trip, while continuations are like a kind of time-machine.

&lt;h2&gt;Closures&lt;/h2&gt;
The general meaning of the term closure in computer science is a function which captures one or more variable bindings in effect at the time it was created.  Closures are created in Ruby using lambda or its alias proc:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;irb(main):001:0&amp;gt; def closure(a)
irb(main):002:1&amp;gt;   lambda {a}
irb(main):003:1&amp;gt;   end
=&amp;gt; nil
irb(main):004:0&amp;gt; cl1 = closure(1)
=&amp;gt; #&amp;lt;Proc:0xb7b3cf10@(irb):2&amp;gt;
irb(main):005:0&amp;gt; cl2 = closure(2)
=&amp;gt; #&amp;lt;Proc:0xb7b3cf10@(irb):2&amp;gt;
irb(main):006:0&amp;gt; cl1.call
=&amp;gt; 1
irb(main):007:0&amp;gt; cl2.call
=&amp;gt; 2&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that the block in line 2 refers to the method parameter a, and returns the value of a as the blocks value.  The Kernel#lamba method turns the block into a Proc object which is returned as the value of the call.&lt;/p&gt;
&lt;p&gt;in lines 4 and 5 we make two calls to the method closure with different arguments, and save the resultant closures in cl1 and cl2 respectively.&lt;/p&gt;
&lt;p&gt;Notice that when we call each closure in lines 6 and 7, each one returns the value passed to cont when that closure was created.
&lt;h2&gt;Continuations&lt;/h2&gt;
A continuation, on the other hand, represents a particular point of execution, not [just] variable bindings.  When a continuation is called, it acts like (the dreaded) goto, we jump to the point right after the creation of the continuation.  In Ruby we create a continuation with the Kernel#callcc method.  This method takes a block and yields it a new continuation object.
&lt;p&gt;The mystical aspect of continuations is that they allow us to jump back into the middle of the execution of a method which has already returned:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;irb(main):008:0&amp;gt; def continuation
irb(main):009:1&amp;gt;   i = 0
irb(main):010:1&amp;gt;   lola = nil
irb(main):011:1&amp;gt;   callcc {|cc| puts &amp;quot;In callcc&amp;quot;;lola = cc}
irb(main):012:1&amp;gt;   puts &amp;quot;i is now #{i}&amp;quot;
irb(main):013:1&amp;gt;   i += 1
irb(main):014:1&amp;gt;   lola
irb(main):015:1&amp;gt;   end
=&amp;gt; nil
irb(main):016:0&amp;gt; cont = continuation
In callcc
i is now 0
=&amp;gt; #&amp;lt;Continuation:0xb7b727a0&amp;gt;
irb(main):017:0&amp;gt; cont.call
i is now 1
=&amp;gt; #&amp;lt;Continuation:0xb7b727a0&amp;gt;
irb(main):018:0&amp;gt; cont.call
i is now 2
=&amp;gt; #&amp;lt;Continuation:0xb7b727a0&amp;gt;                             &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Line 10 makes the ruby interpreter see that lola is a local variable of the method and so the block can set it and the method can return it. &lt;/p&gt;
&lt;p&gt;Note that the call to the continuation method in line 16 is the only time we actually evaluate callcc and the block.  The call to callcc returns and the rest of the continuation method is excuted and the method returns.&lt;/p&gt;
&lt;p&gt;Each time we call the continuation referenced by the variable cont, we execute the code afer the callcc which created it.  It acts like a closure in a sense since it captures the last state of the variables i and lola each time. But this is because the continuation represents a particular execution state which includes not only the variebles in scope at that time, but also the program counter.
&lt;h2&gt;&lt;span class="caps"&gt;FLASH&lt;/span&gt; Breaking News: Rick has a Ruby Red Face&lt;/h2&gt;
&lt;p&gt;Robert Dober pointed out that if you run the above code in ruby rather than in irb it loops on the first cont.call, printing out:
&lt;p&gt;i is now 1&lt;/p&gt;
&lt;p&gt;i is now 1&lt;/p&gt;
&lt;p&gt;i is now 1&lt;/p&gt;
&lt;p&gt;ad infinitum (or at least ad ctrl-C)&lt;/p&gt;
&lt;p&gt;I was actually abusing continuations, since in general they aren&amp;#8217;t meant to be reused.  I&amp;#8217;ll write a new posting in the near future on how continuations are used in the Ruby standard library Generator class, In the meantime, here&amp;#8217;s a simpler example which runs in ruby without trying to reuse the continuation:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;continuation&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Before callcc&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="ident"&gt;callcc&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;lola&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;In callcc&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;;&lt;/span&gt;&lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="ident"&gt;lola&lt;/span&gt;&lt;span class="punct"&gt;}&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;After callcc&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Begin&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="ident"&gt;cont&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;continuation&lt;/span&gt;
&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Got cont &lt;span class="expr"&gt;#{cont.inspect}&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;cont&lt;/span&gt;
  &lt;span class="ident"&gt;cont&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;After call&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;All done&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we run this using ruby we see:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;$ ruby testcontin.rb
Begin
Before callcc
In callcc
Got cont #&amp;lt;Continuation:0xb7de3840&amp;gt;
After callcc
Got cont nil
All done&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Continuations in other disciplines&lt;/h2&gt;
You might get a sense of deja-vu from this discussion of continuations since a similar idea pops up in the movies from time to time. Two examples which come to mind are the American movie &amp;#8220;Groundhog Day,&amp;#8221; and the German movie &amp;#8220;Lola rennt&amp;#8221; or in English, &amp;#8220;Run Lola, Run.&amp;#8221; 
&lt;h2&gt;Another explanation&lt;/h2&gt;
SamRuby &lt;a href="http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons"&gt;this explanation of continuations&lt;/a&gt; a couple of years ago.  It&amp;#8217;s valuable as a companion to this article since it has a somewhat different approach.  I do have a quibble with Sam in that I don&amp;#8217;t agree with his statement that a closure is a &amp;#8220;continuation&#8217;s more general cousin.&amp;#8221;  Some continuations are, or act a little like, closures, but closures aren&amp;#8217;t continuations since they lack the aspect of holding a point of executioncont</description>
      <pubDate>Tue, 05 Jun 2007 21:41:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:728a7e00-76b4-44df-8faa-701c1a006c9e</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-continuations</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>closures</category>
      <category>continuations</category>
    </item>
    <item>
      <title>Chain, Chain, Chain</title>
      <description>&lt;p&gt;I&amp;#8217;ve noticed some confusion lately on the part of  some newcomers to Ruby about how methods are found at runtime, and in particular, the relationship between instance and class methods.&lt;/p&gt;
&lt;p&gt;A week or so ago, someone posted some questions on ruby-talk about this issue, and then today I ran across &lt;a href="http://www.igvita.com/blog/2007/05/08/5-ways-to-sharpen-your-ruby-foo/"&gt;this blog review of David Black&amp;#8217;s &amp;#8220;Ruby for Rails&amp;#8221;&lt;/a&gt; which contained this:&lt;/p&gt;
&lt;blockquote&gt;
On every method call, Ruby will search its object space in the following order:
&lt;ol&gt;
   &lt;li&gt;Current instance, followed by class methods&lt;/li&gt;
   &lt;li&gt;Mixed in methods&lt;/li&gt;
   &lt;li&gt;Superclass instance (repeat 1)&lt;/li&gt;
   &lt;li&gt;Object methods, followed by Kernel mixin&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I often get confused with mixin, class, and instance method precedence, so this is a useful model to revisit and keep in mind.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I don&amp;#8217;t know where that reference to class methods in item 1 came from.  I don&amp;#8217;t &lt;strong&gt;think&lt;/strong&gt; David said that in his book.&lt;/p&gt;
&lt;h2&gt;Here&amp;#8217;s how it really works&lt;/h2&gt;
&lt;p&gt;Here in the form of answers to the questions posed on ruby-talk last week is my explanation of how Ruby finds methods when a message is sent to an object&lt;/p&gt;

&lt;h3&gt; Q: Do all inheritance chains and all objects have a homologue
&amp;gt; metaclass at all times, which are behind the scenes?&lt;/h3&gt;

&lt;p&gt;In ruby metaclasses are singleton classes of classes.  Just as a class
holds the table used to find the methods of its instances, the
metaclass holds the table used to find methods for its sole instance
i.e. the class.&lt;/p&gt;
&lt;p&gt;The singleton class which serves as the metaclass of a class is
created when the class is created.  This is done internally by the
Ruby interpreter.&lt;/p&gt;
&lt;p&gt;While object&amp;#8217;s don&amp;#8217;t have metaclasses they can have singleton classes,
which are created when needed, e.g. when you do something like:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt; a = &amp;quot;abc&amp;quot;
 b = &amp;quot;def&amp;quot;

 def a.method
 end # singleton class for the object referenced by a is created now

 class &amp;lt;&amp;lt;b #singleton class for the object referenced  by b is created now
 end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Q: Do a chain of metaclasses spontaneously appear at the moment when a class is used as a receiver : so to be used to call a method inside of a parentclass?&lt;/h3&gt;
&lt;p&gt;No, as I just said, metaclasses are created along with their
corresponding class.  The metaclass&amp;#8217; superclass is set to the
metaclass of the classes superclass.&lt;/p&gt;
&lt;p&gt;&lt;a name="#clarification"&gt;&lt;/a&gt;Since I first wrote this I&amp;#8217;ve gotten some feedback that the last sentence is a little hard to digest, so here&amp;#8217;s an example. Let&amp;#8217;s say we create a class MyNiftyClass as a subclass of Object.  The singleton class of the class object referenced by the MyNiftyClass global gets created at the same time as class object, no waiting for one of the things which trigger the creation of a singleton class for a non-class object.  Thesuperclass of the MyNiftyClass is set to object, and the superclass of MyNiftyClass&amp;#8217; metaclass is set to Object&amp;#8217;s metaclass.&lt;/p&gt;
&lt;p&gt;Besides the fact that their creation occurs immediately, there is one other difference  between
singleton classes of objects and singleton classes used as
metaclasses. The later can have subclasses, while the former cannot.&lt;/p&gt;
&lt;p&gt;Normal object singleton classes have their internal superclass set to
the original class of the object.&lt;/p&gt;
&lt;p&gt;Note that I&amp;#8217;m using superclass and class here to describe the internal
relationship. Ruby&amp;#8217;s class and superclass methods don&amp;#8217;t always give a
true picture when singleton/metaclasses are involved.  I personally
think of the class-like objects linked together with the superclass
field as a behavior chain since it&amp;#8217;s used to implement the behaviour
of one or more objects.&lt;/p&gt;
&lt;h3&gt;Q: How can a metaclass contain more methods than its corresponding class has.  Can you give an example of the code in the next addition(sic)?&lt;/h3&gt;
&lt;p&gt;The number of methods in the class and the number of methods in the
metaclass is completely independent, for example, look at the
following and compare it to what I said in reponse to question #1&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;MyClass&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt; &lt;span class="comment"&gt;# This defines the initialize method in MyClass which&lt;/span&gt;
                   &lt;span class="comment"&gt;# Like all such methods are available to INSTANCEs&lt;/span&gt;
                   &lt;span class="comment"&gt;# of the class, and its subclasses&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;method1&lt;/span&gt; &lt;span class="comment"&gt;# another instance method&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;MyClass.class_method&lt;/span&gt; &lt;span class="comment"&gt;# This defines a class method in the&lt;/span&gt;
                                   &lt;span class="comment"&gt;# singleton class of MyClass (i.e. the metaclass)&lt;/span&gt;
                                   &lt;span class="comment"&gt;# which is available to instances of the&lt;/span&gt;
                                   &lt;span class="comment"&gt;# metaclass and its subclasses&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;self.class_method_2&lt;/span&gt; &lt;span class="comment"&gt;# This adds another class method, it's&lt;/span&gt;
                                &lt;span class="comment"&gt;# Just a different syntax for the last form of&lt;/span&gt;
                                &lt;span class="comment"&gt;# definition since within the scpe of a class&lt;/span&gt;
                                &lt;span class="comment"&gt;# definition, self is bound to that class&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;   &lt;span class="comment"&gt;# or equivalently class &amp;lt;&amp;lt; MyClass&lt;/span&gt;
                     &lt;span class="comment"&gt;# Now we are in an inner scope, that of MyClass' metaclass&lt;/span&gt;

     &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;class_method_3&lt;/span&gt;  &lt;span class="comment"&gt;# So this also becomes a class method&lt;/span&gt;
                              &lt;span class="comment"&gt;# of MyClass&lt;/span&gt;
     &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So MyClass has two instance methods (plus those it inherits from
Object) and three class methods (plus those the metaclass inherits from
Object&amp;#8217;s metaclass.&lt;/p&gt;
&lt;p&gt;As a related note, when a class includes a Module, another kind of
virtual class is inserted between the class and it&amp;#8217;s superclass.  This
virtual class (or I_Class) looks like a class but has a pointer to the
method  table of the module instead of having its own table.   This
lets modules appear in different behavior chains and lets those
including them see updates to the module when they are made.  There
are some other fields of the module which are also copied, but I&amp;#8217;m not
going to mention them here since they aren&amp;#8217;t relevant and might
confuse.&lt;/p&gt;
&lt;h3&gt;Q: Which is searched first in the inheritance chain.  methods in
the proper parents, or methods in the meta parents?&lt;/h3&gt;
&lt;p&gt;There&amp;#8217;s no cross over at all. When finding a method for an object,
Ruby gets the head of what I called the behavior chain.  For a normal
object with no singleton class, this will be the class, For an object
with a singleton class this will be the singleton class.  If the
method is found, then it&amp;#8217;s used, if not the next class-like thing in
the behavior chain is examined and so forth.  The keyword super makes
the search start in the next link in the behavior chain after the one
in which the current method was found.&lt;/p&gt;
&lt;p&gt;So if you&amp;#8217;re invoking a method on a normal object, no metaclasses will
be on the chain. If you are invoking a method on a class, then the
behavior chain will consist of metaclasses, except that the superclass
of Object&amp;#8217;s metaclass is Class. If you think about this hard enough it
makes sense, since the instance methods of Class are for use by
classes.&lt;/p&gt;
&lt;p&gt;So if we have:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;B&lt;/span&gt;
       &lt;span class="punct"&gt;...&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;b&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;B&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
&lt;span class="ident"&gt;b&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The search goes:&lt;/p&gt;
&lt;blockquote&gt;
B -&amp;gt; Object -&amp;gt; (I_Class wrapper for Kernel)
&lt;/blockquote&gt;
&lt;p&gt;the last is because Object includes Kernel&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;   &lt;span class="constant"&gt;B&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;the search goes&lt;/p&gt;
&lt;blockquote&gt;
(singleton of B) -&amp;gt; (singleton of Object) -&amp;gt; Class -&amp;gt; Module -&amp;gt;Object -&amp;gt; (I_Class wrapper for Kernel)
&lt;/blockquote&gt;
&lt;h3&gt;Q: If a method is found is the search stopped even though another method with the same name might exist in a parallel chain?&lt;/h3&gt;
&lt;p&gt;As I just described, the parallel chain is irrelevant, it isn&amp;#8217;t
considered at all.&lt;/p&gt;</description>
      <pubDate>Sat, 02 Jun 2007 18:28:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:3f50fb28-97da-4d9e-8aae-3edfeb4aa691</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/02/chain-chain-chain</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>implementation</category>
      <category>meta</category>
      <category>singletonclasses</category>
    </item>
  </channel>
</rss>
