<?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: Tag ducks</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/ducks</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>On Ceremony and Training Wheels</title>
      <description>&lt;p&gt;The perennial topic of &lt;a href="http://www.nabble.com/Not-quite-getting-it.-td17281703.html"&gt;&lt;strong&gt;getting&lt;/strong&gt; duck-typing in ruby&lt;/a&gt; has come up again.  A ruby nuby wonders about the dangers of passing a &amp;#8220;giraffe&amp;#8221; object to a method expecting an &amp;#8220;order.&amp;#8221; Or asking an object to &amp;#8220;draw&amp;#8221;, expecting it to be a &amp;#8220;brush&amp;#8221; when it was really a &amp;#8220;cowboy.&amp;#8221;&lt;/p&gt;
&lt;p&gt;These are reasonable questions, coming from someone who is used to a high-ceremony language which gives at least the illusion of protection from such &amp;#8220;errors.&amp;#8221;  Note that method signatures in languages like Java can&amp;#8217;t really protect from a draw method doing different things in different classes.&lt;/p&gt;
&lt;p&gt;J. Cooper offered the following good advice:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Instead, look at it like this: how likely is a Giraffe able to do what
an Order does? If the Giraffe doesn&amp;#8217;t quack like an Order, than an
exception is going to be thrown automatically anyway.&lt;/p&gt;
&lt;p&gt;Just program in it, and see how often it ever actually bites you. Start
with small things, work your way up.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The advice here is to tackle the problem of learning a language like Ruby with a certain amount of fearlessness.  This reminds me of learning to ride a bike.&lt;/p&gt;
&lt;p&gt;The traditional way of learning to ride a bicycle is to use training wheels, sometimes even progressively.  Your dad might have installed the training wheels so that they touched the ground, turning the bike into a kind of tricycle with an extra, overly large back wheel between the normal ones.  You think you&amp;#8217;re riding a bike, but you&amp;#8217;re really still on a trike.&lt;/p&gt;
&lt;p&gt;The next stage, is for dad to raise the training wheels a bit so that you need to start learning to balance properly, but you can&amp;#8217;t really tip too far. Now you&amp;#8217;re riding something that kind of acts like a bicycle, but which &amp;#8220;flops over&amp;#8221; into tricycle mode.&lt;/p&gt;
&lt;p&gt;If you never get past this stage, you never really learn to ride a bike.  In an analogy with programming in Ruby, training wheels are &lt;a href="http://talklikeaduck.denhaven2.com/articles/2007/10/22/chicken-typing-isnt-duck-typing"&gt;chicken typing&lt;/a&gt; devices.&lt;/p&gt;
&lt;p&gt;The truth is that bicycles and motorcycles operate quite differently than wheeled vehicles which keep three or more wheels on the ground, for one thing you steer by leaning, not with the handlebars or steering wheel.  Learning to fly an airplane gives even stronger examples of having to learn that your instincts are wrong, and that you have to train yourself to &amp;#8220;instinctively&amp;#8221; know not only that you turn by banking rather than with the rudder, but that you control altitude primarily with the throttle, not the elevators, speed primarily with the elevators not the throttle, and so forth.&lt;/p&gt;
&lt;p&gt;Now the use of training wheels, or flight simulators, might be considered a ceremony, although a necessary rite of passage for many learners.  Given the high cost of mistakes in learning, it&amp;#8217;s no doubt a good thing that student pilots get time in simulators and in dual control planes accompanied by a flight instructor before soloing.  Although the way most young birds learn to fly is to be throw out of the nest to &amp;#8220;sink&amp;#8221; or &amp;#8220;swim&amp;#8221; so to speak.&lt;/p&gt;
&lt;p&gt;In learning to program, it&amp;#8217;s hard to see a difference between a simulator and reality, so it&amp;#8217;s perfectly feasible to &amp;#8220;fly the plane&amp;#8221; from the start. The consequences of a mistake are no more dire than making a mistake in a flight simulator.  It&amp;#8217;s really just a matter of conquering fear, not to rely on chicken-typing, and the more mistakes you make and correct, the more quickly you come to grips with the differences in really using a dynamically typed language from earlier experiences.&lt;/p&gt;
&lt;p&gt;Some can do this easier than others.  For example, Alex, my 6-year old next-door neighbor recently jumped off his tricycle, and onto a bicycle (sans training wheels) and is now zooming around our cul-de-sac faster than ever before.&lt;/p&gt;
&lt;p&gt;So if you&amp;#8217;re struggling with how to program Ruby or a similar language because lacking ceremonial training wheels to &amp;#8220;bridge the gap&amp;#8221; from Java or a similar language.  Might I suggest that you just jump in like Alex and give it a whirl.&lt;/p&gt;
&lt;p&gt;Of course, it&amp;#8217;s rarely easy to be as fearless as a 6-year old!&lt;/p&gt;</description>
      <pubDate>Sun, 18 May 2008 10:15:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d174206d-505d-465b-8cbd-435bd179a143</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2008/05/18/on-ceremony-and-training-wheels</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>chickens</category>
      <category>ceremony</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/499</trackback:ping>
    </item>
    <item>
      <title>Chicken Typing Isn't Duck Typing</title>
      <description>&lt;div style='width:173; float:left; text-align:right; font-size:xx-small; border-width:1px; border-color:#444444; border-style:solid;margin-bottom:30px; margin-right:30px;' class='tease-image'&gt;
&lt;img width='173' height='240' alt='Stress' src='http://farm1.static.flickr.com/36/95509221_cc5e4aa8f5_m.jpg'&gt;
&lt;br/&gt;
&lt;a href='http://flickr.com/photos/frield/95509221/'&gt;Stress&lt;/a&gt;
&lt;br/&gt;&amp;copy;
&lt;a href='http://flickr.com/people/frield'&gt;David Friel&lt;/a&gt;
&lt;br/&gt;&lt;a href='http://creativecommons.org/licenses/by/2.0/'&gt;&lt;img src='http://i.creativecommons.org/l/by/2.0/80x15.png' title='used under a Creative Commons Attribution License' width='80' height='15' border='0'/&gt;&lt;/a&gt;
&lt;/div&gt;
Trans just posted a &lt;a href="http://thread.gmane.org/gmane.comp.lang.ruby.general/232460"&gt;question on ruby-talk&lt;/a&gt;.
&lt;p&gt;It seems that he uncovered a conflict between the facet's library and the Ruby standard open_uri library.  Open-uri defines a module called Meta which it uses to extend certain StringIo instances. The Meta module adds several methods including one called meta, which returns a hash of meta information.  In two places in open_uri, the message "respond_to? :meta" is used to test whether a particular StringIO instance has been extended.&lt;/p&gt;
&lt;p&gt;The problem is that Facets also defines a method in either Object or Kernel called meta which provides a clever way to access the singleton class of an object.  So the respond_to? in open_uri was getting false positives.&lt;/p&gt;
&lt;p&gt;Trans asked "So where lies the fault in this conflict? Are extensions the bad guy,
or is respond_to? really not a good oop concept? Or..?"&lt;/p&gt;
&lt;p&gt;David Black replied that he thinks "this is just the age-old issue about adding methods to
existing classes and hitting conflicts. #respond_to? is relatively
dumb; it can't tell you what the method actually does. I think of it
as an important tool for "soft" duck typing, in contrast with "hard"
duck typing where you just send the object the message.
"&lt;/p&gt;
&lt;p&gt;Reading this made me realize that a much better name than "soft" duck typing, for using kind_of?/is_a?, respond_to?, or other such "sniff first" techniques, is &lt;strong&gt;Chicken Typing&lt;/strong&gt; &lt;/p&gt;



&lt;h2&gt;Chicken Typing Isn't The Dynamic Equivalent of Static Typing&lt;/h2&gt;
&lt;p&gt;From what I've observed chicken typing is a "safety blanket" technique used by programmers experienced in statically typed languages, who seem uncomfortable with making the paradigm shift needed to really make dynamically typed languages work.&lt;/p&gt;
&lt;p&gt;But because Ruby is dynamically typed, such techniques work imperfectly at best.  Here are &lt;strong&gt;some&lt;/strong&gt; of the problems:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Checking with respond_to? can give false positives like this because just because an object has a method with the name you are looking for, doesn't mean that the method has the semantics you expect&lt;/li&gt;
	&lt;li&gt;Similarly respond_to? can give false negatives when methods are implemented dynamically via method_missing.  Chicken typers try to enforce conventions which require overriding respond_to? to get around this, but in general this is difficult, expensive, or impossible to do perfectly.&lt;/li&gt;
	&lt;li&gt;Checking with kind_of?/is_a? is unnecessarily constraining. In Ruby you don't need the the equivalent to Java Interfaces or Abstract Classes, and once you've really made the paradigm shift from chicken typing to duck typing, you &lt;a href="http://blog.objectmentor.com/articles/2007/08/08/craftsman-51-brown-bag-viii-ruby-visitor"&gt;realize that you don't want them&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;What's OpenURI Doing?&lt;/h2&gt;
&lt;p&gt;Because of Trans' question, I decided to look at how OpenURI was using respond_to? in this case. Here's the code in question:&lt;/p&gt;
First the beginnings of the OpenURI::Meta module
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;OpenURI&lt;/span&gt;
 &lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Meta&lt;/span&gt;
   &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;Meta.init&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;nil&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;# :nodoc:&lt;/span&gt;
     &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;extend&lt;/span&gt; &lt;span class="constant"&gt;Meta&lt;/span&gt;
     &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;instance_eval&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;
       &lt;span class="attribute"&gt;@base_uri&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;nil&lt;/span&gt;
       &lt;span class="attribute"&gt;@meta&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{}&lt;/span&gt;
     &lt;span class="punct"&gt;}&lt;/span&gt;
     &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;
       &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;status&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;status&lt;/span&gt;
       &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;base_uri&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;base_uri&lt;/span&gt;
       &lt;span class="ident"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;meta&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;each&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
         &lt;span class="ident"&gt;obj&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;meta_add_field&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
       &lt;span class="punct"&gt;}&lt;/span&gt;
     &lt;span class="keyword"&gt;end&lt;/span&gt;
   &lt;span class="keyword"&gt;end&lt;/span&gt;

   &lt;span class="ident"&gt;attr_accessor&lt;/span&gt; &lt;span class="symbol"&gt;:status&lt;/span&gt;
   &lt;span class="ident"&gt;attr_accessor&lt;/span&gt; &lt;span class="symbol"&gt;:base_uri&lt;/span&gt;

   &lt;span class="comment"&gt;# returns a Hash which represents header fields.&lt;/span&gt;
   &lt;span class="comment"&gt;# The Hash keys are downcased for canonicalization.&lt;/span&gt;
   &lt;span class="ident"&gt;attr_reader&lt;/span&gt; &lt;span class="symbol"&gt;:meta&lt;/span&gt;

   &lt;span class="comment"&gt;# MORE STUFF....&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt; &lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Meta.init causes the object passed as the first parameter to extend Meta, if a second parameter is passed, it's presumably also a Meta, and it's meta attributes are copied to obj.&lt;/p&gt;
&lt;p&gt;Now here's the use of chicken typing:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;OpenURI&lt;/span&gt;
   &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Buffer&lt;/span&gt; &lt;span class="comment"&gt;# :nodoc:&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;
      &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;StringIO&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
      &lt;span class="attribute"&gt;@size&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="ident"&gt;attr_reader&lt;/span&gt; &lt;span class="symbol"&gt;:size&lt;/span&gt;

    &lt;span class="constant"&gt;StringMax&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;10240&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;str&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;str&lt;/span&gt;
      &lt;span class="attribute"&gt;@size&lt;/span&gt; &lt;span class="punct"&gt;+=&lt;/span&gt; &lt;span class="ident"&gt;str&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;length&lt;/span&gt;
      &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="constant"&gt;StringIO&lt;/span&gt; &lt;span class="punct"&gt;===&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="constant"&gt;StringMax&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="attribute"&gt;@size&lt;/span&gt;
        &lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;tempfile&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
        &lt;span class="ident"&gt;io&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Tempfile&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;open-uri&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
        &lt;span class="ident"&gt;io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;binmode&lt;/span&gt;
        &lt;span class="constant"&gt;Meta&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;init&lt;/span&gt; &lt;span class="ident"&gt;io&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;respond_to?&lt;/span&gt; &lt;span class="symbol"&gt;:meta&lt;/span&gt;
        &lt;span class="ident"&gt;io&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;string&lt;/span&gt;
        &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;io&lt;/span&gt;
      &lt;span class="keyword"&gt;end&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;io&lt;/span&gt;
      &lt;span class="constant"&gt;Meta&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;init&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt; &lt;span class="keyword"&gt;unless&lt;/span&gt; &lt;span class="attribute"&gt;@io&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;respond_to?&lt;/span&gt; &lt;span class="symbol"&gt;:meta&lt;/span&gt;
      &lt;span class="attribute"&gt;@io&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt; &lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A few observations:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;The variable which is being chicken typed, in both cases, is an instance variable which is set by the initialize method and never rebound.  So there's no concern about checking an object which was handed in, e.g. through a parameter.  We might be concerned that someone outside the class might somehow access it and change it to extend Meta without the Buffer knowing about it, but...&lt;/li&gt;
	&lt;li&gt;The instance variable will &lt;strong&gt;always&lt;/strong&gt; extend Meta after the accessor is called, so the above concern is moot.&lt;/li&gt;
	&lt;li&gt;In the io accessor method, respond_to? is used to prevent re-extending Meta.  This could be avoided by either extending the object referred to by @io in the initialize method, or, if there's a legitimate reason to defer the extension, either adding a state instance variable to indicate that the extension has been done, or just change "unless @io.respond_to? :meta" to "unless @io.kind_of?(Meta)". This is one case where kind_of? is really useful since we're really just checking if code under our control has extended an object under our control, with a particular Module.&lt;/li&gt;
	&lt;li&gt;Finally, a minor point, but I find this code unnecessarily hard to read because of the choice of name 'io for the temporary in the &amp;lt.&amp;lt. method.  Look at the line "io.binmode".  Which io are we talking about?  Is it the io we just assigned, or the result of the io accessor method. Now it happens to be the former, but knowing that requires knowing that the assignment above is what causes the Ruby parser to treat it as a temporary rather than a method send.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Better a Duck Than  a Chicken&lt;/h2&gt;
&lt;p&gt;So in my opinion, it's better to be a bold Duck than a timid Chicken.  Chicken typing is weak, and it really doesn't provide much more than the "warm fuzzies".  Duck type, and build your specs/tests as you code to provide long-term robustness in dynamically typed languages.&lt;/p&gt;</description>
      <pubDate>Mon, 22 Oct 2007 10:43:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:234b2578-fe29-4215-994e-73d9592cfd63</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/22/chicken-typing-isnt-duck-typing</link>
      <category>ruby</category>
      <category>best_practices</category>
      <category>ducks</category>
      <category>chickens</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/475</trackback:ping>
    </item>
    <item>
      <title>Duck A La Range</title>
      <description>&lt;div style='width:240; float:left; text-align:right; font-size:xx-small; border-width:1px; border-color:#444444; border-style:solid;margin-bottom:30px; margin-right:30px;' class='tease-image'&gt;
&lt;img src="http://talklikeaduck.denhaven2.com/files/2007-09-20_turducken_1.png" alt="Turducken photo by Rainer Zenz, from Wikimedia Commons" height="128" width="112" class="tease-image"&gt;
&lt;br/&gt;
&lt;a href='http://en.wikipedia.org/wiki/Image:Turducken.jpg'&gt;Turducken&lt;/a&gt;
&lt;br/&gt;
&lt;a href='http://en.wikipedia.org/wiki/User:Zerokaneda'&gt;Public  Domain&lt;br/&gt;via Wikimedis Commons&lt;/a&gt;
&lt;/a&gt;
&lt;/div&gt;

On ruby-talk, Sammy Larbi recently asked if it would make sense to add a length method to Range, something like this:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;class Range
     def length
          self.end - self.begin
     end
end&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This led to an interesting discussion about the varieties of ranges, and ultimately led me to this article about duck types.&lt;/p&gt;



&lt;p&gt;This led to an observation by Xavier Noria that&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Ranges don't require their elements to support a "-" method, only :succ and :&lt;=&gt;, so this wouldn't work for all ranges.&lt;/li&gt;
	&lt;li&gt;A first approximation of a general implementation of Range#length would be to iterate over the elements of the range and count them but..&lt;/li&gt;
	&lt;li&gt;Some ranges might actually have an infinite number of elements, so length doesn't make sense for them.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Home, Home on the Infinite Range&lt;/h2&gt;
&lt;p&gt;In thinking about this a bit, I'm not sure that you can have a Range with an infinite number of elements which supports enumeration.  Xavier gave a range of rational numbers as an example,  but I'm not sure I see how you can define both a :&lt;=&gt;, and :succ methods for rational numbers which make sense.  The problem is that although rational numbers are countable, they are also densely ordered.&lt;/p&gt;
&lt;p&gt;The fact that rationals are countable simply means that you can pair each rational number with an integer.  One proof of this constructs the sequence: 1/1, 2/1, 1/2, 1/3, 3/1, 4/1, 3/2, ...&lt;/p&gt;
&lt;p&gt;Now this shows that you can put the rationals into an infinite sequence, and define :succ such that (1/1).succ =&gt; (2/1), but if you want to keep the normal meaning of :&lt;=&gt;, you have the problem that 2/1 looks like it is &lt;strong&gt;less&lt;/strong&gt; than 1/2.&lt;/p&gt;
&lt;p&gt;The fact that rationals are densely ordered means that for any two rationals a &amp;lt. b there are an &lt;strong&gt;infinite&lt;/strong&gt; number of rationals, c, such that a &amp;lt. c &amp;lt b.  Which makes it hard to pick which one should be a.succ.&lt;/p&gt;
&lt;p&gt;Now it might be possible to come up with an enumerable infinite range which works, but it hurts my head, and it's not really the main point of this article.&lt;/p&gt;
&lt;h2&gt;What About Non-enumerable Ranges?&lt;/h2&gt;
&lt;p&gt;The rdoc for Range includes this:
&lt;blockquote&gt;
Ranges can be constructed using objects of any type, as long as the objects can be compared using their &lt;=&gt; operator and they support the succ method to return the next object in sequence.
&lt;/blockquote&gt;
&lt;p&gt;This is true, if you want to use all of the methods of Range, but one can actually usefully create ranges of objects with support &lt;=&gt; but don't support succ.&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;1.0.methods.include?(:succ) # =&amp;gt; false
(1.0..2.0).include?(1.5) # =&amp;gt; true&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Floats respond to &lt;=&gt;, but they don't respond to succ.  As shown above you can create a Float range, and use it to see if a number falls within that Range.  What you can't do is enumerate such a Range:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;(1.0..2.0).to_a # =&amp;gt; 
# ~&amp;gt; -:3:in `each': can't iterate from Float (TypeError)
# ~&amp;gt; 	from -:3:in `to_a'
# ~&amp;gt; 	from -:3&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So what does this have to do with duck typing?  It just goes to serve as another illustration of why classes make lousy duck types.  It's not just a matter of what an objects class is, in general it's a matter of what the object can do at the time, and that can depend on things other than the class and the methods.  The duck in this last example is a bird which can determine whether or not it includes a number, it simply has to quack yes or no when fed a number.  It doesn't have to walk along all the numbers it 'contains.'&lt;/p&gt;
&lt;p&gt;Whether or not such a duck is useful depends on the user of the duck, not the duck itself.&lt;/p&gt;
&lt;h2&gt;About That Picture&lt;/h2&gt;
&lt;p&gt;I searched around to find a non-copyrighted photo of Duck a l'orange, and couldn't find one.  I did find one of a Turducken.  For those unfamiliar with this bird, it's a boneless turkey, which has been stuffed with a boneless duck, which has in turn been stuffed with a boneless chicken.  After thinking about this a bit it seemed appropriate for this article which is after all about the effects of stuffing one object with another.&lt;/p&gt;</description>
      <pubDate>Thu, 20 Sep 2007 11:36:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:5d0099b7-c342-4e98-b554-2f33c920669c</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/09/20/duck-a-la-range</link>
      <category>ruby</category>
      <category>ducks</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/464</trackback:ping>
    </item>
    <item>
      <title>Our Kind of Ducks, Odd Ducks, and Trained Ducks</title>
      <description>&lt;p&gt;A couple of days ago, someone asked on ruby-talk how to implement a cyclic version of Enumerable#each_cons.  In words, he was looking for a method:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Array
   def each_cycle(size, start=0)
      #insert implementation here
   end
end

# So that this:
%{a b c d e}.each_cycle(2) {|cyc| p cyc}

#would print:
[&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;]
[&amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]
[&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;]
[&amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;]
[&amp;quot;e&amp;quot;, &amp;quot;a&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As usual, several helpful rubyists responded with suggestions, one of which leads me to &lt;span class="caps"&gt;YADTBE&lt;/span&gt; (Yet Another Duck Typing Blog Entry)&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s one suggested solution:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;module Enumerable
  def each_cycle(window, start=0)
    (start...length+start).each do |i|
      yield((i...i+window).map {|n| self[n % length]})
    end
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Nice solution, but as soon as I saw it, I saw a problem.&lt;/p&gt;
&lt;p&gt;While this will work if the method were defined in the Array class, it doesn&amp;#8217;t work in general for Enumerables. Let&amp;#8217;s write a Kernel method to try some different Enumerables.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;def try(enum)
  puts &amp;quot;Trying this #{enum.class} ==&amp;gt; #{enum.inspect}&amp;quot;
  begin
    enum.each_cycle(2) {|cyc| p cyc}
  rescue =&amp;gt; ex
    puts ex
  end
  puts
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Is it a duck?&lt;/h2&gt;
&lt;p&gt;Okay, now that we&amp;#8217;ve done that, let&amp;#8217;s try some enumerables:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(%w{and a one and a two})

# Results in
Trying this Array ==&amp;gt; [&amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;one&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;two&amp;quot;]
[&amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;]
[&amp;quot;a&amp;quot;, &amp;quot;one&amp;quot;]
[&amp;quot;one&amp;quot;, &amp;quot;and&amp;quot;]
[&amp;quot;and&amp;quot;, &amp;quot;a&amp;quot;]
[&amp;quot;a&amp;quot;, &amp;quot;two&amp;quot;]
[&amp;quot;two&amp;quot;, &amp;quot;and&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So this works if the receiver is an Array.&lt;/p&gt;
&lt;h2&gt;Not the kind of duck we&amp;#8217;re looking for&lt;/h2&gt;
&lt;p&gt;Ranges are Enumerable, so let&amp;#8217;s try one.&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(1..5)
# Results in
Trying this Range ==&amp;gt; 1..5
undefined local variable or method `length' for 1..5:Range&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Aye, there&amp;#8217;s the rub, the each_cycle method assumes that the receiver implements both a length method, and a [] method.&lt;/p&gt;
&lt;p&gt;This is the kind of requirement which leads some &amp;#8220;strong ducktypers&amp;#8221; to use a respond_to? test.  But does that really work?&lt;/p&gt;
&lt;h2&gt;Odd ducks&lt;/h2&gt;
&lt;p&gt;Well, Hash implements both of those methods, so it would pass the &amp;#8216;responds_to?&amp;#8217; test. Lets try one:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(Hash[*(%w{zero, one, two, three}.zip((0..3).to_a)).flatten])
# Which prints:
Trying this Hash ==&amp;gt; {&amp;quot;two,&amp;quot;=&amp;gt;2, &amp;quot;three&amp;quot;=&amp;gt;3, &amp;quot;one,&amp;quot;=&amp;gt;1, &amp;quot;zero,&amp;quot;=&amp;gt;0}
[nil, nil]
[nil, nil]
[nil, nil]
[nil, nil]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Hmmm, not exactly what we are looking for.  So it&amp;#8217;s not enough to just be an enum with those two methods.  We&amp;#8217;ve just exposed another requirement of Enumeration#each_cycle, that the receiver be indexed by integers.  We&amp;#8217;re getting all nils because all of those nicely generated indices don&amp;#8217;t have corresponding values in the hash, and hash is lenient about indices (i.e. keys) which aren&amp;#8217;t keys.&lt;/p&gt;
&lt;p&gt;So what happens if we try a hash which &lt;strong&gt;does&lt;/strong&gt; have integer keys:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(Hash[*((1..3).to_a.zip(%w{one, two, three}).flatten)])
#now we get
Trying this Hash ==&amp;gt; {1=&amp;gt;&amp;quot;one,&amp;quot;, 2=&amp;gt;&amp;quot;two,&amp;quot;, 3=&amp;gt;&amp;quot;three&amp;quot;}
[nil, &amp;quot;one,&amp;quot;]
[&amp;quot;one,&amp;quot;, &amp;quot;two,&amp;quot;]
[&amp;quot;two,&amp;quot;, nil]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, that sort of works, but it exposes yet another requirement, that the indices of the enum be in the range (0&amp;#8230;enum.size).&lt;/p&gt; 
&lt;h2&gt;I dub thee Sir Duck&lt;/h2&gt;
&lt;p&gt;If we use a slightly different hash:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;try(Hash[*((0..3).to_a.zip(%w{zero, one, two, three}).flatten)])
# We get
Trying this Hash ==&amp;gt; {0=&amp;gt;&amp;quot;zero,&amp;quot;, 1=&amp;gt;&amp;quot;one,&amp;quot;, 2=&amp;gt;&amp;quot;two,&amp;quot;, 3=&amp;gt;&amp;quot;three&amp;quot;}
[&amp;quot;zero,&amp;quot;, &amp;quot;one,&amp;quot;]
[&amp;quot;one,&amp;quot;, &amp;quot;two,&amp;quot;]
[&amp;quot;two,&amp;quot;, &amp;quot;three&amp;quot;]
[&amp;quot;three&amp;quot;, &amp;quot;zero,&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So for a hash which looks enough like an Array, the code in Enum actually works.&lt;/p&gt;
&lt;h2&gt;Teaching a range to be our kind of Duck&lt;/h2&gt;
&lt;p&gt;One final example.  Let&amp;#8217;s revisit the case of a range.  We can easily train the range class, or using a singleton class an individual instance of range how to support the each_cycle method:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Range
  def length
    (last - first) + (exclude_end? ? 0 : 1)
  end

  def [](i)
    first + i
  end
end

# Now if we try this again:
try(1..5)

# We get:
Trying this Range ==&amp;gt; 1..5
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 1]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Okay Rick, what&amp;#8217;s your point?&lt;/h2&gt;
&lt;p&gt;Although this exercise might seem a little contrived, it points out that in a dynamically &amp;#8216;typed&amp;#8217; language like Ruby, whether or not a particular object is suitable for a particular use can depend not only on it&amp;#8217;s class, or which modules are mixed in, or whether or not it responds to a particular set of messages, but on &lt;strong&gt;how&lt;/strong&gt; it responds to those messages.&lt;/p&gt;
&lt;p&gt;Using my metaphor of casting for a role in a play, finding the right actor isn&amp;#8217;t just a matter of finding someone who went to a particular acting school, (i.e. has (had) the right class or classes), but it&amp;#8217;s a matter of fitting the individual to the requirements.  It&amp;#8217;s akin to being interviewed for a job, for the employer doing a successful &amp;#8216;type-check&amp;#8217; is a subtle process, and often leads to a probation period (i.e. testing).&lt;/p&gt;
&lt;p&gt;Strong-typing advocates will no doubt see this as a flaw in dynamic systems.  I see it as a strength.  If you think about it honestly, there are a wide variety of requirements which aren&amp;#8217;t expressed in strongly typed language, either because the type system doesn&amp;#8217;t choose to express subsets of types, such as positive numbers, prime numbers, even numbers, etc.; or because state-oriented requirements such as an open file, don&amp;#8217;t really fit into the notion of a static type.&lt;/p&gt;
&lt;p&gt;In my experience, I&amp;#8217;ve gotten to the point where I rarely make the kind of errors which are caught by static types, and the flexibility of dynamic languages far outweighs the relatively minor benefit of early static type checking.  I&amp;#8217;d rather rely on a growing set of test cases to fight bugs.&lt;/p&gt;</description>
      <pubDate>Mon, 30 Apr 2007 16:01:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:56f0fa9a-c597-404b-ab22-5d5da4d93ffe</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/04/30/our-kind-of-ducks-odd-ducks-and-trained-ducks</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
    </item>
    <item>
      <title>Hungarian Ducks</title>
      <description>&lt;p&gt;It&amp;#8217;s interesting how events flow sometimes.&lt;/p&gt;
&lt;p&gt;One of my major interests outside of software development is human
spaceflight.  Today I opened the local paper to the coming events
section and discovered that the latest &amp;#8216;rich cosmonaut&amp;#8217; to buy a trip
up to the international space station, was arriving there today, and
that his name was Charles Simonyi. As I&amp;#8217;m writing this, he has just
become the first Hungarian in space, or at least that&amp;#8217;s what I heard
him say over &lt;span class="caps"&gt;NASA TV&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;Update: Actually that honor went to &lt;a href="http://en.wikipedia.org/wiki/Bertalan_Farkas"&gt;Bertalan Farkas&lt;/a&gt; who flew on Soyuz 36 in May of 1980.&lt;/p&gt;
&lt;p&gt;Now that&amp;#8217;s a familiar name.  So I spent a little time this morning
looking at his article on Wikipedia, and following some links.
Thereby hangs an interesting tale which relates to duck-typing.&lt;/p&gt;

&lt;h2&gt;Charles Simonyi&lt;/h2&gt;&lt;p&gt;Simonyi was the software architect at the Microsoft Applications
Division back in the early days of Excel and Word.  Before coming to
Microsoft he was at Xerox Parc, at the same time as folks like Alan
Kay, Butler Lampson, Robert Metcalfe, and Dan Ingalls. One of his most
famous inventions was so-called Hungarian Notation, a way of encoding
&amp;#8216;type&amp;#8217; information into the names of variables. This became very
popular among C programmers following the Microsoft programming
bibles.&lt;/p&gt;
&lt;p&gt;Now I never much cared for Hungarian Notation (which I&amp;#8217;ll refer to
simply as Hungarian for the rest of this article).  As normally
practiced it led to variable names which looked like they&amp;#8217;d grabbed
onto part of the C compiler&amp;#8217;s symbol table entry. The variables would
look like iX, and iY (for two different integer variables), or something like pudwGorp for a
pointer to an unsigned doubleword.&lt;/p&gt;
&lt;p&gt;Eventually Hungarian fell out of favor as more and more programmers
realized that it didn&amp;#8217;t contribute enough to pull its weight in ugly
names.&lt;/p&gt;
&lt;h2&gt;Hungarian Dialects&lt;/h2&gt;
&lt;p&gt;What I learned today was that this form of Hungarian is actually a corruption of Simonyi&amp;#8217;s
original Hungarian which, instead of encoding the machine
representation of the variable, encodes the &lt;strong&gt;intentional
use&lt;/strong&gt; of the variable. The Hungarian which most people
saw was that used by the Microsoft Systems Division.  Those in the
know refer to this as Systems Hungarian to distinguish it from
Simonyi&amp;#8217;s original notation which is known by insiders as Apps Hungarian.&lt;/p&gt;
&lt;p&gt;Here, from &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs600/html/hunganotat.asp"&gt;Simonyi&amp;#8217;s original explanation of Hungarian Notation&lt;/a&gt; is how
variables (or as he called them quantities) should be named:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Quantities are named by their type possibly followed by a
qualifier. A convenient (and legal) punctuation is recommended to
separate the type and qualifier part of a name. (In C, we use a
capital initial for the qualifier as in rowFirst: row is the type;
First is the qualifier.)&lt;/li&gt;
&lt;li&gt;Qualifiers distinguish quantities that are of the same type and
that exist within the same naming context. Note that contexts may
include the whole system, a block, a procedure, or a data structure
(for fields), depending on the programming environment. If one of the
&amp;#8220;standard qualifiers&amp;#8221; is applicable, it should be used. Otherwise, the
programmer can choose the qualifier. The choice should be simple to
make, because the qualifier needs to be unique only within the type
and within the scope&#8212;a set that is expected to be small in most cases.
In rare instances more than one qualifier may appear in a name.
Standard qualifiers and their associated semantics are listed below.
An example is worthwhile: rowLast is a type row value; that is, the
last element in an interval. The definition of Last states that the
interval is &amp;#8220;closed&amp;#8221;; that is, a loop through the interval should
include rowLast as its last value.&lt;/li&gt;
&lt;li&gt;Simple types are named by short tags that are chosen by the
programmer. The recommendation that the tags be small is startling to
many programmers. The essential reason for short tags is to make the
implementation of rule 4 realistic. Other reasons are listed
below.&lt;/li&gt;
&lt;li&gt;Names of constructed types should be constructed from the names of
the constituent types. A number of standard schemes for constructing
pointer, array, and different types exist. Other constructions may be
defined as required. For example, the prefix p is used to construct
pointers. prowLast is then the name of a particular pointer to a row
type value that defines the end of a closed interval. The standard
type constructions are also listed below.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;State Your Intentions&lt;/h2&gt;
&lt;p&gt;Note that Simonyi here is really talking about the intentional
&lt;strong&gt;use&lt;/strong&gt; of the variable rather than it&amp;#8217;s representation
as a bag of bits.&lt;/p&gt;
&lt;p&gt;While this isn&amp;#8217;t really a duck-typing system, it relates in that it
describes the usage of a variable in priority to the machine
representation.  Since the host language here is C, the notation still
has to talk about things like pointers, and whether or not the pointer
is being used as an array.&lt;/p&gt;
&lt;p&gt;So in Apps Hungarian instead of iX, and iY, those variables would
likely just be x and y if they were being used for x and y positions, or row and col for spreadsheet coordinates.
Other names might be wrx for an x position relative to the window
coordinate system, or wrxWidget1 for a particular window relative x
coordinate.&lt;/p&gt;
&lt;p&gt;And the two dialects are incompatible.  As &lt;a href="http://blogs.msdn.com/rick_schaut/archive/2004/02/14/73108.aspx"&gt;explained by Rick Schaut:&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
For those of us who have grown accustomed to Simonyi-style HN, the whole &#8220;i&#8221; means &#8220;int&#8221; prefix thing is doubly horrible, because the &#8220;i&#8221; prefix in Simonyi-style HN is commonly understood to be an index into an array. If I put a character into an int, I&#8217;m not going to call it iCh. I&#8217;m just going to call it ch. To me, an ich is an index to a character in an array of characters.
&lt;/blockquote&gt;
&lt;p&gt;In comparison to System Hungarian, this seems to be much more useful information than a shorthand encoding of the underlying C datatype.&lt;/p&gt;
&lt;p&gt;This schism between the two dialects came about because Simonyi
used the word &lt;strong&gt;type&lt;/strong&gt; where a better word such as
&lt;strong&gt;usage&lt;/strong&gt; or &lt;strong&gt;role&lt;/strong&gt; would have better
represented the idea.  Perhaps this came from the fact that Simonyi&amp;#8217;s
native language was, not surprisingly, Hungarian rather than
English.  It was actually the technical writers in the Systems
Division who created Systems Hungarian by editing earlier
documentation of the concept without fully understanding it.&lt;/p&gt;
&lt;p&gt;It seems to me that this is the same confusion which confounds so
many discussions of duck-typing in dynamic languages.&lt;/p&gt;
&lt;h2&gt;Further Reading&lt;/h2&gt;
&lt;p&gt;&lt;a href="http://www.byteshift.de/msg/hungarian-notation-doug-klunder"&gt;Another
Microsoft document on Hungarian naming conventions&lt;/a&gt; by Doug
Klunder.&lt;/p&gt;
&lt;p&gt;More explanations of the  &lt;a href="http://blogs.msdn.com/larryosterman/archive/2004/06/22/162629.aspx#163721"&gt;difference between App Hungarian and
System Hungarian have been given byLarry
Osterman&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/rick_schaut/archive/2004/02/14/73108.aspx"&gt;Rick
Schaut&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.joelonsoftware.com/articles/Wrong.html"&gt;Joel
Spoelsky&lt;/a&gt; also wrote about this.  Although I often disagree with
Joel on specific topics, he makes some interesting points.  One thing
that struck me in this article was his use of Hungarian prefixes &amp;#8217;s&amp;#8217;,
and &amp;#8216;us&amp;#8217; to represent safe and unsafe variables in a web application
to indicate which variables should be distrusted and escaped before
being used to render html.  On the surface this seems like a nice idea
if applied consistently, although I&amp;#8217;d still want to make use of
mechanisms like Ruby&amp;#8217;s unsafe tainting of objects to give real
safety.&lt;/p&gt;
&lt;p&gt;You see I just don&amp;#8217;t think that source-code time checking is powerful
enough to trust it completely&lt;/p&gt;</description>
      <pubDate>Mon, 09 Apr 2007 17:07:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:561cd646-bba9-4b54-9b8e-3c6cd76c2d9b</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/04/09/hungarian-ducks</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
    </item>
    <item>
      <title>Schr&#246;dinger's Duck</title>
      <description>&lt;p&gt;Once again there&amp;#8217;s been a recent furore about the meaning of Ducktyping on the ruby-lang mailing list.&lt;/p&gt;


	&lt;p&gt;A nice young man from Sweden, by the name of Ola Bini, announced &amp;#8220;ductator&amp;#8221; a ruby-gem which allows type validation of
ruby variables.  Now I think that Ola is young based on the photo on his blog site, and I &lt;b&gt;know&lt;/b&gt; that he&amp;#8217;s nice because only
nice people inhabit ruby-lang.&lt;/p&gt;


	&lt;p&gt;The real question is just what kind of a type system ductator represents.  I&amp;#8217;ve taken a brief look at it and it seems to allow one
to describe, via a hash, a list of rules which are  applied to a variable&amp;#8217;s value at run-time. These rules are things like
a list of methods the value must have, a class which the value must be an instance of, or an instance of a subclass of etc.&lt;/p&gt;


&lt;h2&gt;The Cat is Dead, Long Live the Cat&lt;/h2&gt;
In 1935 Erwin Schr&#246;dinger, proposed his famous thought experiment, in which a cat, a a radioactive atomic nucleus, and a
cannister of poison gas rigged ot open if the nucleus decayedare placed in a sealed box. The half-life of the neucleus is
such that there is a probability of 1/2 of the nucleus decaying within one hour.  Because quantum mechanics states that
until a decay is observed, the nucleus really exists as a superposition of decayed and undecayed states, that the cat is
both dead and alive until the box is open and the observation is made.

	&lt;p&gt;Schr&#246;dinger&amp;#8217;s experiment is really a dramatized version of a similar thought experiment previously published in a paper
co-written by Einstein.  Although Einstein intended it as a refutation of the probabilistic nature of quantum mechanics,
the stochastic basis of quantum mechanics has held up over the ensuing seven decades, and such paradoxes have held up.&lt;/p&gt;


	&lt;p&gt;My point here is that, in a sense, the kind of type checker represented by ducktator.  It attempts to observe certain
aspects of an object, and in so doing, has a good chance of killing the duck.&lt;/p&gt;


	&lt;p&gt;Besides the questions of what duck-typing &lt;b&gt;is&lt;/b&gt; or &lt;b&gt;is not&lt;/b&gt;, such a set of checks fails under many common usages in ruby.&lt;/p&gt;


&lt;h2&gt;False Negatives&lt;/h2&gt;
Relying on &lt;b&gt;responds_to?&lt;/b&gt; fails under commonly used Ruby metaprogramming techniques.

	&lt;p&gt;It&amp;#8217;s very common these days to use &lt;b&gt;method_missing&lt;/b&gt; to provide pseudo-methods, or to
create methods on the fly.  ActiveRecord and other RubyOnRails componentry makes extensive use
of this technique.&lt;/p&gt;


	&lt;p&gt;The problem is, of course, that an instance of a class which uses these techniques effectively
gives an invalid response to &lt;b&gt;responds_to?&lt;/b&gt;, or may reply false, &lt;i&gt;until&lt;/i&gt; the missing
method is invoked, creating the method.&lt;/p&gt;


	&lt;p&gt;Poking around a bit, I notice that some of the standard library classes, and others liek ActiveRecord, go to some lengths to make responds_to? track with &lt;i&gt;phantom&lt;/i&gt; methods implemented via &lt;b&gt;:method_missing&lt;/b&gt;, although fixing other reflection methods like &lt;b&gt;:methods&lt;/b&gt; and the class method &lt;b&gt;:instance_methods&lt;/b&gt; doesn&amp;#8217;t seem to be commonly done.
While this is good, it can greatly increase the expense of &lt;b&gt;:responds_to?&lt;/b&gt;. ActiveRecord::Base, aliases &lt;b&gt;:responds_to?&lt;/b&gt; as &lt;b&gt;responds_to_without_attributes?&lt;/b&gt; before redefining &lt;b&gt;:responds_to?&lt;/b&gt; for this reason.&lt;/p&gt;


	&lt;p&gt;In the case of Drb::DrbObject, which is used as the proxy object in Distributed Ruby, handling &lt;b&gt;:respond_to?&lt;/b&gt; on behalf of a remote object, requires marshalling a &lt;b&gt;:respond_to?&lt;/b&gt; message to the remote object, sending the marshalled message, then reading and unmarshalling the reply.  This makes sending &lt;b&gt;:respond_to?&lt;/b&gt; to a remote object &lt;b&gt;much&lt;/b&gt; more expensive.&lt;/p&gt;


And irony of ironies, ductator uses &lt;b&gt;:method_missing&lt;/b&gt; to supply &lt;i&gt;phantom&lt;/i&gt; methods in &lt;b&gt;it&amp;#8217;s&lt;/b&gt;
implementation, without implementing a compensating &lt;b&gt;:respond_to?&lt;/b&gt;!
&lt;h2&gt;False Positives&lt;/h2&gt;
Even if one accepts the philosopy posited by ductator, using it can get tricky. For example, one might check to see if an object &lt;b&gt;responds_to?(:each)&lt;/b&gt;, but exactly what &lt;b&gt;:each&lt;/b&gt; does and how it interacts with it&amp;#8217;s block argument, depends on the class itself. For example, most enumerables yield a single value to the block in &lt;b&gt;:each&lt;/b&gt;, but Hash yields both a key and a value.
The block can be written to accomodate this, but it doesn&amp;#8217;t have to be.  It&amp;#8217;s not clear to me how
to either express or check the type of such block arguments.  I don&amp;#8217;t see a mechanism in
ductator even to check the arity of a proc, and although this could be added, it&amp;#8217;s not really
sufficient since it doesn&amp;#8217;t express the types required of block arguments.
&lt;h2&gt;What&amp;#8217;s the Value?&lt;/h2&gt;
Some folks might find something like ductator to their taste, since it give the impression of making Ruby more like Java or C++. The problem is that in so doing, it makes Ruby less like Ruby.

This kind of type checking seems to be an expensive add-on to the real checking which happens
during program execution.  The fact that everything is an object in ruby means that the really
nasty errors in a language like C when you use an integer as a pointer don&amp;#8217;t happen.  It seems
to me that the best that something like ductator can do is to make a more &amp;#8216;meaningful&amp;#8217; error
description when something goes wrong, but doing that correctly is harder than it first appears,
and in most cases could be accomplished more efficiently by catching errors with rescue clauses
and changing the error reporting, after the fact, which puts the cost of the error on the
offender rather than taxing everyone with run-time pre-flight tests.
&lt;h2&gt;Wrapping up for now&lt;/h2&gt;
The French word for &amp;#8220;duck&amp;#8221; is &amp;#8220;canard.&amp;#8221; 

	&lt;p&gt;In computer jargon, the word &amp;#8220;canard&amp;#8221; means a &lt;a href="http://en.wikipedia.org/wiki/Canard_(computing)"&gt;&amp;#8220;mistaken and confused belief&amp;#8221;&lt;/a&gt;. In my humble opinion, the view of duck typing taken by ducktator and its ilk is in this sense really &amp;#8220;canard&amp;#8221; typing.&lt;/p&gt;


	&lt;p&gt;Le canard est mort, vive long le canard!&lt;/p&gt;


	&lt;p&gt;Or translated into English:&lt;/p&gt;


	&lt;p&gt;The canard is dead, long live the duck!&lt;/p&gt;</description>
      <pubDate>Thu, 21 Sep 2006 11:27:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:77fd8187-b151-4ee5-a7a3-d08e3c414c42</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/09/21/schr%C3%B6dingers-duck</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/32</trackback:ping>
    </item>
    <item>
      <title>I'm Ready For My Close-up, Mr. Demille</title>
      <description>&lt;p&gt;Back when I was younger, and object[-oriented] programming was fairly new, my colleagues and I seemed to spend a lot of time philosophizing about types and objects.  Although I don&amp;#8217;t remember talking about ducks, we did talk a lot about the theater.  The most common word we used to use for what we now call a duck type was a role.&lt;/p&gt;


	&lt;p&gt;Another way of looking at this, is that duck typing is akin to the way a person is chosen to fill the role of the American President. An assessment is performed on the candidates,one is chosen, and then the country, and world adapt.&lt;/p&gt;


	&lt;p&gt;Contrast this to the way things are done in picking the next British monarch, or the type systems of C++ or Java.  In both cases one has to be born into the role, having the right genes via inheritance. Of course in the first case, who gets the job doesn&amp;#8217;t matter all that much anymore.&lt;/p&gt;


	&lt;p&gt;Recently a thread about patterns on the ruby-talk mailing list turned, as threads there are wont to do, to duck-typing.&lt;/p&gt;


William Crawford, helpfully contributed:
&lt;blockquote&gt;
In short, if a type/class/whatever has all the methods of a duck (that
you intend to use), it&amp;#8217;s a duck and can be used as a duck would be used,
regardless of what&amp;#8217;s actually under the feathers.

	&lt;p&gt;&lt;br/&gt;
In other words, TCPSocket doesn&amp;#8217;t just inherit from Socket, Object,
etc&amp;#8230;  It IS a Socket, it IS an Object, etc.
&lt;/blockquote&gt;&lt;/p&gt;


	&lt;p&gt;While this is true, the second paragraph is not a particularly good example to explain duck typing.&lt;/p&gt;


	&lt;p&gt;Duck typing is not about what an object &amp;#8220;is&amp;#8221;, but what it can be used for.  Whether an object can be used for something depends on who is using it.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s look at DT&amp;#8217;s example from the Pickaxe.  Cutting the code down to the bare essentials we&amp;#8217;ve got:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;def csv_from_row(op, row)
   #code to compute res from row
   op &amp;lt;&amp;lt; res &amp;lt;&amp;lt; CRLF
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Now what kind of duck does op need to be?  Simply an object which accumulates objects via the &amp;lt;&amp;lt; method.&lt;/p&gt;


	&lt;p&gt;His first use was:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;result = &amp;quot;&amp;quot;
query.each_row {|row| csv_from_row(result, row)
http.write result
&amp;lt;/type:code&amp;gt;

This works, but it's slow as the number of rows gets large. Strings meet the requirements of the op parameter, but they also have other characteristics. The important one here is the subtle effect on GC.  For a more detailed analysis of this see why's article http://whytheluckystiff.net/articles/theFullyUpturnedBin.html

So let's try to find another object which we can put in the role of the op parameter.  Arrays also can be used.  So we have

&amp;lt;typo:code lang=&amp;quot;ruby&amp;quot;&amp;gt;
result = []
query.each_row {|row| csv_from_row(result, row)
http.write result&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;However we&amp;#8217;ve now got a little problem in that although an array works as the op parameter, http.write probably won&amp;#8217;t like it.  But that&amp;#8217;s easy to fix, just change the last line slightly to accomodate the new duck in the outer context:&lt;/p&gt;


&lt;typo:code lang="ruby"&gt;
http.write result.join
&lt;/type:code&gt;

	&lt;p&gt;So, getting back to the theater analogy, when we duck type, we&amp;#8217;re in a position similar to a director casting for a role in a play.  Sometimes, we find a great actor, but we need to subtly adapt the script to fit his style.  That&amp;#8217;s what&amp;#8217;s happening in DT&amp;#8217;s pickaxe example.&lt;/p&gt;</description>
      <pubDate>Tue, 22 Aug 2006 11:37:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:e7cda932-8026-4f4d-b907-6796b76399e7</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/08/22/im-ready-for-my-close-up-mr-demille</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>roles</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/25</trackback:ping>
    </item>
    <item>
      <title>Ducks Can Be Subtle Birds</title>
      <description>&lt;p&gt;One theory I&amp;#8217;ve seen defines a &amp;#8220;duck type&amp;#8221; as a set of messages which an object bound to a parameter or value needs to understand.  This leads some, who want to make type-checking happen a bit earler, to propose testing the values of such variables with oneor more respond_to? tests before using the object &amp;#8220;in anger.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;But ducks can be subtle&amp;#8230;&lt;/p&gt;


	&lt;p&gt;Sometimes it&amp;#8217;s not enough just to have a certain set of methods in a duck&amp;#8217;s repetoire.&lt;/p&gt;


Let&amp;#8217;s look at an, admittedly cooked-up, example:
&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;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;dictionary&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
     &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;dictionary&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;collect&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;kv&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;span class="punct"&gt;==&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt; &lt;span class="punct"&gt;?&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="constant"&gt;nil&lt;/span&gt; &lt;span class="punct"&gt;}).&lt;/span&gt;&lt;span class="ident"&gt;compact&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
The method is intended to return an array containing
keys in a &amp;#8220;Dictionary&amp;#8221; which map to the given value:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;irb(main):001:0&amp;gt; load 'reverse_lookup.rb'
=&amp;gt; true

irb(main):002:0&amp;gt; reverse_lookup(1, { 'a' =&amp;gt; 1, 'b' =&amp;gt; 2, 'c' =&amp;gt; 1})
=&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;c&amp;quot;]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Now, I said that this was a &amp;#8220;cooked-up&amp;#8221; example to illustrate a point about &amp;#8220;ducks.&amp;#8221; It would probably be more proper to implement a method like keys_for_value in Hash or a module which could be used to extend Hash or other &amp;#8220;dictionary&amp;#8221; classes.

	&lt;p&gt;The point here is that for this implementation of reverse_lookup, the only message sent to dictionary is collect, soaccording to the &amp;#8220;respond_to?&amp;#8221; theory any object which has &amp;#8220;collect&amp;#8221; in it&amp;#8217;s repetoire is the right species of duck for the dictionary parameter.&lt;/p&gt;


So let&amp;#8217;s try another duck:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;irb&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;main&lt;/span&gt;&lt;span class="punct"&gt;):&lt;/span&gt;&lt;span class="number"&gt;003&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;reverse_lookup&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;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;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;])&lt;/span&gt;
&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Well, it works in the sense that nothing blows up, but the results look strange, where did that 0 come from?&lt;/p&gt;


While you weren&amp;#8217;t looking, I snuck a debug parameter into my reverse_lookup method to give an &amp;#8220;x-ray&amp;#8221; view into what&amp;#8217;s happening.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;irb&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;main&lt;/span&gt;&lt;span class="punct"&gt;):&lt;/span&gt;&lt;span class="number"&gt;004&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;reverse_lookup&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;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;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;],&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="ident"&gt;kv&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;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&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;span class="ident"&gt;kv&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;span class="number"&gt;0&lt;/span&gt;
&lt;span class="ident"&gt;kv&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;]=&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&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;span class="number"&gt;1&lt;/span&gt;
&lt;span class="ident"&gt;kv&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;kv&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&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;span class="ident"&gt;kv&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;span class="number"&gt;1&lt;/span&gt;
&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
The reason that it works at all with an array of integers is a bit of an anomally due to the fact that Ruby integers define the [] method as a bit reference. When kv is an integer kv&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt; and kv&lt;sup&gt;&lt;a href="#fn0"&gt;0&lt;/a&gt;&lt;/sup&gt; are respectively the least significant, and second least significant bits in the binary representation of kv.

	&lt;p&gt;Now let&amp;#8217;s try another array;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;irb&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;main&lt;/span&gt;&lt;span class="punct"&gt;):&lt;/span&gt;&lt;span class="number"&gt;005&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;reverse_lookup&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;span class="punct"&gt;[&lt;/span&gt;&lt;span class="constant"&gt;Array&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="constant"&gt;NilClass&lt;/span&gt;&lt;span class="punct"&gt;])&lt;/span&gt;
&lt;span class="constant"&gt;NoMethodError&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="ident"&gt;undefined&lt;/span&gt; &lt;span class="ident"&gt;method&lt;/span&gt; `&lt;span class="punct"&gt;[]'&lt;/span&gt;&lt;span class="string"&gt; for NilClass:Class
        from ./reverse_lookup.rb:2:in `reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
        &lt;span class="ident"&gt;from&lt;/span&gt; &lt;span class="punct"&gt;./&lt;/span&gt;&lt;span class="ident"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;rb&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="symbol"&gt;:in&lt;/span&gt; `&lt;span class="ident"&gt;reverse_lookup&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;
        from (irb):4
        from :0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
So this doesn&amp;#8217;t work at all.

	&lt;p&gt;To sum this up, &amp;#8220;duck types&amp;#8221; aren&amp;#8217;t just collections of messages, whether or not an object will work in any given role also depends on subtleties of the semantics of theimplementation of the corresponding methods, and can also depend on the state of the object as well.&lt;/p&gt;


	&lt;p&gt;Now this shouldn&amp;#8217;t be considered a weakness of &amp;#8220;duck typing&amp;#8221; compared to &amp;#8220;strong typing,&amp;#8221; or something in-between the two. Strong-typing systems have similar &amp;#8220;flaws&amp;#8221;  for example strongly typing a variable to an integer won&amp;#8217;t protect against division by zerowhcn the variable is used as a divisor, nor will it alone protect against out of bounds errors when it&amp;#8217;s used as an array index.  The errors which aren&amp;#8217;t detected by strong-typing is a much larger set than those which are.  In languages which provide limited run-time checking of code which has passed the strong-typing &amp;#8220;hurdle&amp;#8221; the effects can be disastrous. In my humble opinion, the benefits of dynamic typing far outweigh &amp;#8220;unlearning&amp;#8221; what has been taught by other languages such as C++ and Java.&lt;/p&gt;


	&lt;p&gt;Bjarne Stroustrup used to quip, in panel discussions over strong vs. dynamic typing (i.e C++ vs. Smalltalk), that he&amp;#8217;d hate to be a passenger on a plane which flashed a light labled &amp;#8220;message not understood&amp;#8221; in the cockpit when the pilot tried to lower the landing gear.  I&amp;#8217;d hate even more to be a passenger on a plane whose control software seg-faulted under this condition instead because putting the landing gear down caused a buffer overflow.&lt;/p&gt;


	&lt;p&gt;The real answer to uncovering and routing out errors is testing, testing, and more testing.&lt;/p&gt;</description>
      <pubDate>Thu, 10 Aug 2006 11:09:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:57838eb5-1cde-4cad-8ad9-8aecb88af12c</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/08/10/ducks-can-be-subtle-birds</link>
      <category>ruby</category>
      <category>ducks</category>
      <category>types</category>
      <category>ruby</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/7</trackback:ping>
    </item>
  </channel>
</rss>
