<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/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>Alice, Bob, and Mallory: Category Ruby</title>
    <link>http://www.alicebobandmallory.com/articles/category/ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>Leif in Clojure</title>
      <description>&lt;p&gt;I've been meaning to learn a functional language for years. I don't know what put me off besides the fear of parentheses and the notion that maybe, just maybe, &lt;a href="http://www.randomhacks.net/articles/2005/12/03/why-ruby-is-an-acceptable-lisp"&gt;Ruby could be an acceptable Lisp&lt;/a&gt;. For all I know, Ruby might be just that, but there's  one thing about Ruby that is starting to get more and more unacceptable to me. &lt;/p&gt;

&lt;p&gt;Ruby is actually not very well fit to take advantage of the multi-core world we are living in. The &lt;a href="http://en.wikipedia.org/wiki/Global_Interpreter_Lock"&gt;GIL&lt;/a&gt; and the prevalent mutable state are two big problems. As web applications go, that's often not a problem since it's almost always possible to scale without threads there. But if you want to use the full potential of your multi-core machine in a singe Ruby process/VM, you are out of luck. There are &lt;a href="https://github.com/tarcieri/celluloid"&gt;workarounds&lt;/a&gt; that get you somewhere and there are implementations of Ruby without a GIL (JRuby and soon Rubinius). Even without the GIL we still have the problem of having a lot of mutable state. That will, probably, make it hard to scale over multiple cores.&lt;/p&gt;

&lt;p&gt;A little more than a week ago I watched the presentation &lt;a href="http://www.infoq.com/presentations/Simple-Made-Easy"&gt;Simple Made Easy by Rich Hickey&lt;/a&gt;. It's an excellent presentation and one of the best I've seen in years. I was also aware that Hickey is the man who designed &lt;a href="http://en.wikipedia.org/wiki/Clojure"&gt;Clojure&lt;/a&gt;. Finally, I was able to make a choice. I chose to begin to learn &lt;a href="http://clojure.org/"&gt;Clojure&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm still very early in this endeavor. Actually, still trying to decide what books I should read. Anyway, the day after I saw the presentation I downloaded &lt;a href="https://github.com/arthuredelstein/clooj"&gt;clooj&lt;/a&gt; (a simple IDE), read some blogs and some documentation, and started to hack on something. Just to try to get a feel for how hard this was going to be. The parentheses didn't bother me all that much, it was much harder to accept that operators are just functions and that they get no special treatment. It seems you simply have to (+ 31 11) to add 31 and 11. I think I can get over that.&lt;/p&gt;

&lt;p&gt;As a side note, it turns out I wrote my first Clojure program on 2011-10-24 and that happened to be the day that &lt;a href="http://en.wikipedia.org/wiki/John_McCarthy_(computer_scientist)"&gt;John McCarthy&lt;/a&gt;, the father of Lisp, died. Yet another of those strange coincidences that &lt;a href="http://youarenotsosmart.com/2010/09/11/the-texas-sharpshooter-fallacy/"&gt;seems to happens more often than they should&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The program I wrote is yet another implementation of the stupid simple "algorithm" that I've got to call &lt;a href="http://plea.se/me/Leif.html"&gt;Leif&lt;/a&gt;. If you take &lt;a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life"&gt;Conway's Game of Life&lt;/a&gt;, remove the rules and replace them with randomness, you could end up with something like &lt;a href="http://plea.se/me/leif/canvas_leif.html"&gt;Leif&lt;/a&gt;. Quite silly, actually.&lt;/p&gt;

&lt;p&gt;Hopefully I will have something more interesting to tell in a couple of &lt;strike&gt;weeks&lt;/strike&gt;months, after I've got some more experience with Clojure. For now, Leif will have to do. Here it is.&lt;/p&gt;

&lt;script src="https://gist.github.com/1335248.js"&gt; &lt;/script&gt;

&lt;p&gt;This is what it looks like at the beginning  &lt;/p&gt;

&lt;p&gt;&lt;img src="http://dl.dropbox.com/u/26840/clojure_leif_couple_of_tenths_of_a_second.png"&gt;&lt;/p&gt;

&lt;p&gt;and after a couple of minutes it looks like this  &lt;/p&gt;

&lt;p&gt;&lt;img src="http://dl.dropbox.com/u/26840/clojure_leif_couple_of_minutes_in.png"&gt;  &lt;/p&gt;

&lt;p&gt;One thing that surprised me is that it seems to use both of my cores even though I put no effort whatsoever in making it do so. Probably just the Java GC or something.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://dl.dropbox.com/u/26840/clojure_leif_task_manager.png"&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 01 Nov 2011 03:12:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:2716bd0d-7bde-43bb-930b-320e824a41ce</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2011/11/01/leif-in-clojure</link>
      <category>Ruby</category>
      <category>Clojure</category>
    </item>
    <item>
      <title>Inspired by randomness</title>
      <description>&lt;p&gt;Inspired by &lt;a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html"&gt;Randomly not so random&lt;/a&gt; I decided to play around with the subject.&lt;/p&gt;

&lt;p&gt;Both Java and C# uses a &lt;a href="http://en.wikipedia.org/wiki/Linear_congruential_generator"&gt;linear congruential generator&lt;/a&gt; as their &lt;a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator"&gt;pseudorandom number generators&lt;/a&gt;. I found &lt;a href="http://msdn.microsoft.com/en-us/library/system.random.aspx"&gt;this&lt;/a&gt;
 at MSDN &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;but I couldn't find any information on what values Microsoft uses for the &lt;em&gt;m&lt;/em&gt;, &lt;em&gt;a&lt;/em&gt;, and &lt;em&gt;c&lt;/em&gt; parameters in their &lt;a href="http://en.wikipedia.org/wiki/Linear_congruential_generator"&gt;LGC&lt;/a&gt; implementation.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;The German Federal Office for Information Security (BSI) has established four criteria for quality of deterministic random number generators. They are summarized here:
    K1 — A sequence of random numbers with a low probability of containing identical consecutive elements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From the &lt;a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator"&gt;pseudorandom number generator&lt;/a&gt; article on Wikipedia. Let's see what we can do about that.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; random = new Random(&lt;span style="color:#00D;font-weight:bold"&gt;116793166&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; i = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; i &amp;lt; &lt;span style="color:#00D;font-weight:bold"&gt;9&lt;/span&gt;; i++)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    Console.Write(random.Next(&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;) + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;p&gt;Outputs:&lt;/p&gt;

&lt;pre&gt;
1 1 1 1 1 1 1 1 1
&lt;/pre&gt;

&lt;p&gt;Did I just break the K1 criteria above? You know I didn't but you might be interested in how I found the seed number. It's easy but also quite computational intensive, that's why I used &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for.aspx"&gt;Parallel.For&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;Parallel.For(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;.MaxValue, i =&amp;gt;&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; rnd = new Random(i);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; allSame = &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; j = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; j &amp;lt; &lt;span style="color:#00D;font-weight:bold"&gt;9&lt;/span&gt;; j++)&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        allSame = allSame &amp;amp;&amp;amp; rnd.Next(&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;) == &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (!allSame) &lt;span style="color:#080;font-weight:bold"&gt;break&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (allSame)&lt;tt&gt;
&lt;/tt&gt;        Console.WriteLine(i); &lt;tt&gt;
&lt;/tt&gt;});&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
It took a couple of minutes for the number 116793166 to show up in my console.&lt;/p&gt;

&lt;p&gt;To keep on playing I needed a random string generator. Mine looks like this.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;private &lt;span style="color:#080;font-weight:bold"&gt;const&lt;/span&gt; string _chars = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;private &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; string RandomString(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; size, Random rng)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; buffer = new &lt;span style="color:#339;font-weight:bold"&gt;char&lt;/span&gt;[size];&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; i = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; i &amp;lt; size; i++)&lt;tt&gt;
&lt;/tt&gt;        buffer[i] = _chars[rng.Next(_chars.Length)];&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; new string(buffer);&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
If the random generators were the same it looks like it should give the same result as &lt;a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html"&gt;the Java one&lt;/a&gt; but &lt;code&gt;RandomString(5, new Random(-229985452))&lt;/code&gt; returns &lt;code&gt;tfsld&lt;/code&gt;. Either my RandomString method works different than the Java one or it's the case that Java and .NET has slightly different settings of their LGCs.&lt;/p&gt;

&lt;p&gt;Mathematically there's an infinite amount of inputs that results in a returned &lt;code&gt;hello&lt;/code&gt;, for instance 1382472294, but here we are limited by the size of our integers.&lt;/p&gt;

&lt;p&gt;I found the seed 1382472294 with the following little method and loop&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;private &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; CompareToRandomString(string str, Random rng) &lt;tt&gt;
&lt;/tt&gt;{ &lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; i = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; i &amp;lt; str.Length; i++)&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (_chars[rng.Next(_chars.Length)] != str[i])&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;}&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;...&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;const&lt;/span&gt; string lookingFor = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;hello&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;Parallel.For(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;.MaxValue, i =&amp;gt;&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; rnd = new Random(i);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (CompareToRandomString(lookingFor, rnd))&lt;tt&gt;
&lt;/tt&gt;        Console.WriteLine(i);&lt;tt&gt;
&lt;/tt&gt;});&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
I leave it up to you to implement a &lt;a href="http://msdn.microsoft.com/en-us/library/dd460721.aspx"&gt;break&lt;/a&gt; out of that loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.ruby-doc.org/core-1.9.2/Random.html"&gt;Ruby&lt;/a&gt; does not use a LGC but a &lt;a href="http://en.wikipedia.org/wiki/Mersenne_twister"&gt;Mersenne twister&lt;/a&gt; instead. It's still only pseudorandom so there's no problem finding patters you like and being able to repeat them.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;srand(&lt;span style="color:#00D;font-weight:bold"&gt;2570940381&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#00D;font-weight:bold"&gt;9&lt;/span&gt;.times { print &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;%d &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; % rand(&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;) }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Outputs:&lt;br/&gt;
&lt;code&gt;1 2 3 4 5 6 7 8 9&lt;/code&gt;&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;charset=(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;a&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;..&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;z&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;).to_a&lt;tt&gt;
&lt;/tt&gt;srand(&lt;span style="color:#00D;font-weight:bold"&gt;124931&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;puts (&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;...&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;).map{ charset.to_a[rand(charset.size)] }.join&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Outputs:&lt;br/&gt;
&lt;code&gt;ruby&lt;/code&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 27 Oct 2011 23:19:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:f596c241-0004-4935-ba79-64693f890cb2</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2011/10/27/inspired-by-randomness</link>
      <category>Ruby</category>
      <category>C#</category>
      <category>Math</category>
      <category>Java</category>
    </item>
    <item>
      <title>Sudoku solver in CoffeeScript</title>
      <description>&lt;p&gt;&lt;a href="http://jashkenas.github.com/coffee-script/"&gt;CoffeeScript&lt;/a&gt; is inspired by Ruby and Python but what's most peculiar with it is that it compiles to JavaScript. The generated JavaScript isn't all that bad and it even passes &lt;a href="http://www.javascriptlint.com/"&gt;JavaScript lint&lt;/a&gt; without warnings.&lt;/p&gt;

&lt;p&gt;"Underneath all of those embarrassing braces and semicolons, JavaScript has always had a gorgeous object model at its heart." - Jeremy Ashkenas&lt;/p&gt;

&lt;p&gt;About a week ago I stumbled on the very clever &lt;a href="http://norvig.com/sudoku.html"&gt;Sudoku solver by Peter Norvig&lt;/a&gt;. I have nothing (or at least not much) against Python but I pretty soon checked out the Ruby translations of the solver. I then &lt;a href="https://gist.github.com/914326"&gt;refactored one&lt;/a&gt; of the solutions to get a chance to get to know the algorithm better.&lt;/p&gt;

&lt;p&gt;Now that I finally &lt;a href="http://jashkenas.github.com/coffee-script/#installation"&gt;installed CoffeeScript&lt;/a&gt; the Sudoku solver came to mind. I dived in head first and got in trouble pretty soon. It turns out that Array Comprehensions in CoffeeScript differs some from the List Comprehensions in Python.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;cross&lt;/span&gt;(&lt;span style="color:#036;font-weight:bold"&gt;A&lt;/span&gt;, &lt;span style="color:#036;font-weight:bold"&gt;B&lt;/span&gt;):&lt;tt&gt;
&lt;/tt&gt;       &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Cross product of elements in A and elements in B.&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;       &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; [a+b &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; a &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#036;font-weight:bold"&gt;A&lt;/span&gt; &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; b &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#036;font-weight:bold"&gt;B&lt;/span&gt;]&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;returns an one-dimensional array if you call it with two arrays (or strings).&lt;/p&gt;

&lt;p&gt;But in CoffeeScript &lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;cross = (&lt;span style="color:#036;font-weight:bold"&gt;A&lt;/span&gt;, &lt;span style="color:#036;font-weight:bold"&gt;B&lt;/span&gt;) -&amp;gt; (a+b &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; a &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#036;font-weight:bold"&gt;A&lt;/span&gt; &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; b &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#036;font-weight:bold"&gt;B&lt;/span&gt;)&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;returns a two-dimensional array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/jashkenas/coffee-script/issues/1191"&gt;The jury&lt;/a&gt; is still out on &lt;a href="http://stackoverflow.com/questions/5685449/nested-array-comprehensions-in-coffeescript"&gt;if this is intended&lt;/a&gt; or not but either way the &lt;a href="http://en.wikipedia.org/wiki/List_comprehension"&gt;array  comprehensions&lt;/a&gt; in CoffeeScript are still very useful.&lt;/p&gt;

&lt;div style="background-color: #F0F0F0"&gt;
&lt;font color="red"&gt;EDIT 2011-06-02&lt;/font&gt;&lt;br&gt;
It's been decided that this is by design. The issue has been closed.
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 For the &lt;code&gt;cross&lt;/code&gt;-function I ended up with&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;cross = (&lt;span style="color:#036;font-weight:bold"&gt;A&lt;/span&gt;, &lt;span style="color:#036;font-weight:bold"&gt;B&lt;/span&gt;) -&amp;gt;&lt;tt&gt;
&lt;/tt&gt;  results = []&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; a &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#036;font-weight:bold"&gt;A&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; b &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#036;font-weight:bold"&gt;B&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;      results.push a + b&lt;tt&gt;
&lt;/tt&gt;  results&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;div style="background-color: #F0F0F0"&gt;
&lt;font color="red"&gt;EDIT 2011-06-02&lt;/font&gt;&lt;br&gt;
Using `map` I could have got much closer to the Ruby version.
&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;cross = (cols, rows) -&amp;gt;&lt;tt&gt;
&lt;/tt&gt;  [].concat (cols.map (x) -&amp;gt; rows.map (y) -&amp;gt; y+x)...&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

I'm still more of a map/reduce guy than a list comprehension ninja.
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
You can find the &lt;a href="https://gist.github.com/927782"&gt;CoffeScript Sudoku solver as a Gist&lt;/a&gt;. &lt;strike&gt;Compile it with&lt;br/&gt;
&lt;code&gt;coffee -c sudoku.coffee&lt;/code&gt;&lt;br/&gt;
and then run it with&lt;br/&gt;
&lt;code&gt;node sudoku.js&lt;/code&gt;&lt;/strike&gt;&lt;/p&gt;

&lt;p&gt;I don't know how I missed it but as Trevor pointed out below all you have to do is&lt;br/&gt;
&lt;code&gt;coffee sudoku.coffee&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;
    1 2 3 4 5 6 7 8 9
  |------+-----+-----|
A | 4 8 3|9 2 1|6 5 7|
B | 9 6 7|3 4 5|8 2 1|
C | 2 5 1|8 7 6|4 9 3|
  |------+-----+-----|
D | 5 4 8|1 3 2|9 7 6|
E | 7 2 9|5 6 4|1 3 8|
F | 1 3 6|7 9 8|2 4 5|
  |------+-----+-----|
G | 3 7 2|6 8 9|5 1 4|
H | 8 1 4|2 5 3|7 6 9|
I | 6 9 5|4 1 7|3 8 2|
  |------+-----+-----|
&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://alicebobandmallory.com/sudoku.js.html"&gt;Here's&lt;/a&gt; the generated &lt;a href="http://alicebobandmallory.com/sudoku.js.html"&gt;sudoku.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is the only CoffeeScript I've ever written but I already like it (more than JavaScript). Please correct me if I strayed from the CoffeeScript way. &lt;/p&gt;</description>
      <pubDate>Tue, 19 Apr 2011 15:46:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:12b7c916-c5eb-4510-ac0a-0efcfd241225</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2011/04/19/sudoku-solver-in-coffeescript</link>
      <category>Ruby</category>
      <category>JavaScript</category>
    </item>
    <item>
      <title>A strict directed graph</title>
      <description>&lt;p&gt;Recently N. asked a question on a list that made my geek-senses tingle. He had a test with 12 levels of questions. The testee should answer 10 questions. If a question was answered correctly the level would go up and if it was not correct it would go down. If a level 12 was answered correctly, another level 12 would be asked and vice versa. The first question would be a level 6. N. wanted to know how many questions of each level he needed.&lt;/p&gt;

&lt;p&gt;Pretty soon he got a correct answer and several wrong. One of the incorrect answers was, and I don't like to admit this, provided by me. I took the recursive route (which I very seldom do) and then tried to present the result in ASCII. I failed. The result from the algorithm in itself was correct but the presentation was simplified so much it failed to deliver the correct answer. Here it is:&lt;pre&gt;
             06
            05 07
          04 06 08
         03 05 07 09
       02 04 06 08 10
      01 03 05 07 09 11
    01 02 04 06 08 10 12
   01 02 03 05 07 09 11 12
 01 02 03 04 06 08 10 11 12
01 02 03 04 05 07 09 10 11 12
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;The problem is that this result can be read as if the testee could get two level 2 questions in a row but the rules forbids that.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://plea.se/g.png" target="_blank"&gt;&lt;img title="Click to show full size." src="http://plea.se/me/pics/RootedBinaryDirectedAcyclicGraph.png" style="display: inline-block; float:right" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then I remembered reading about &lt;a href="http://www.graphviz.org/"&gt;Graphviz&lt;/a&gt; and especially &lt;a href="http://github.com/glejeune/Ruby-Graphviz/"&gt;Ruby-Graphviz&lt;/a&gt;. That's what I used to generate the graph to the right. The graph itself is nothing but a visualization of all the possible question sequences. To find the answer that N. was looking for you can follow a level from top to bottom and count the number of times it can occur. I have to admit that it's far from the most convenient way but it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result&lt;/strong&gt;&lt;br/&gt;
5 of level 1, 5, 6 &amp;amp; 7.&lt;br/&gt;
4 of level 3, 4, 8, 9  &amp;amp; 12.&lt;br/&gt;
3 of level 2, 10 &amp;amp; 11.&lt;/p&gt;

&lt;p&gt;I had to jump a few hurdles to get there. First my old Ubuntu-install, for unknown reasons, had some ancient version of &lt;code&gt;libfreetype&lt;/code&gt; in &lt;code&gt;/usr/local/lib/&lt;/code&gt; that I had to remove. In the end an &lt;code&gt;rm /usr/local/lib/libfreetype*&lt;/code&gt; would have been good. I also did a &lt;code&gt;sudo ldconfig&lt;/code&gt; but I'm not sure that was necessary. Let's just hope you're on a modern system where a &lt;code&gt;sudo apt-get install graphviz&lt;/code&gt; followed by &lt;code&gt;sudo gem install ruby-graphviz&lt;/code&gt; will be enough.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://gist.github.com/639589"&gt;recursive code&lt;/a&gt; I wrote produces every single path the testee can take. A graph of that would be quite big since it's 1023 nodes. I then &lt;a href="http://www.graphviz.org/Documentation.php"&gt;found out&lt;/a&gt; about &lt;em&gt;strict digraph&lt;/em&gt; in the Graphviz &lt;a href="http://en.wikipedia.org/wiki/DOT_language"&gt;DOT language&lt;/a&gt;. It sounded like a perfect fit and it was! One problem though, Ruby-Graphviz didn't seem to know about it. The power of open source software let me patch &lt;code&gt;ruby-graphviz-0.9.18/lib/graphviz/constants.rb&lt;/code&gt; so that &lt;code&gt;GRAPHTYPE&lt;/code&gt; included it&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#666"&gt;## Const: graphs type&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt; &lt;span style="color:#036;font-weight:bold"&gt;GRAPHTYPE&lt;/span&gt; = [&lt;tt&gt;
&lt;/tt&gt;   &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;digraph&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;graph&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;strict digraph&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt; ]&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
Another awesome power of open source is &lt;a href="http://algorithmique.net/about.html"&gt;Grégoire Lejeune&lt;/a&gt; because only hours after me contacting him he &lt;a href="http://github.com/glejeune/Ruby-Graphviz/commit/b8d10d28e0c2a58e57bab4fe9c859b114e02e8a8"&gt;added it&lt;/a&gt; to the GitHub repository.&lt;/p&gt;

&lt;p&gt;Here's the code that produced the Rooted Binary &lt;a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph"&gt;Directed Acyclic Graph&lt;/a&gt; (or is it Rooted Directed Binary Acyclic Graph?).&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;require &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;rubygems&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;require &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;graphviz&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@min_level&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@max_level&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@max_depth&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;start_level=&lt;span style="color:#00D;font-weight:bold"&gt;6&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@g&lt;/span&gt; = &lt;span style="color:#036;font-weight:bold"&gt;GraphViz&lt;/span&gt;.new(&lt;span style="color:#A60"&gt;:G&lt;/span&gt;, &lt;span style="color:#A60"&gt;:type&lt;/span&gt; =&amp;gt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;strict digraph&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; )&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;add_node&lt;/span&gt;(level, depth, parent)&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; depth&amp;lt;&lt;span style="color:#33B"&gt;@max_depth&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    current=[level, depth].join(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;,&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    sub=level&amp;lt;=&amp;gt;&lt;span style="color:#33B"&gt;@min_level&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    add=&lt;span style="color:#33B"&gt;@max_level&lt;/span&gt;&amp;lt;=&amp;gt;level&lt;tt&gt;
&lt;/tt&gt;    add_node(level-sub, depth+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, current)&lt;tt&gt;
&lt;/tt&gt;    add_node(level+add, depth+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, current)&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@g&lt;/span&gt;.add_node(current).label=level.to_s&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@g&lt;/span&gt;.add_edge(parent, current) &lt;span style="color:#080;font-weight:bold"&gt;unless&lt;/span&gt; parent==&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;00&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;add_node(start_level, &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;00&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@g&lt;/span&gt;.output( &lt;span style="color:#A60"&gt;:png&lt;/span&gt; =&amp;gt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;graph.png&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; )&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
With the current parameters this problem might not be the most exciting but I believe that by just modifying them slightly we would, again, reach a wall of complexity.&lt;/p&gt;</description>
      <pubDate>Sun, 24 Oct 2010 00:39:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:c534b2b5-4895-4979-af10-6673ee6810b9</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2010/10/24/a-strict-directed-graph</link>
      <category>Ruby</category>
      <category>Math</category>
    </item>
    <item>
      <title>A simple loop</title>
      <description>&lt;p&gt;There's more than one way to skin a cat and the same is true for looping in Ruby. This is a silly post with a silly number of ways to  &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;print the integers from 1 to 10.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're a BASIC-programmer and are getting your feet wet with Ruby,  you might end up with something like this.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;while&lt;/span&gt; i&amp;lt;=&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt; &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;   puts i&lt;tt&gt;
&lt;/tt&gt;   i+=&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
That and the following &lt;code&gt;for&lt;/code&gt;-loop is not the usual Ruby way of looping.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; i &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt; puts i&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
Instead rubyists often iterates over &lt;a href="http://ruby-doc.org/core/classes/Range.html"&gt;ranges&lt;/a&gt; or &lt;a href="http://ruby-doc.org/core/classes/Array.html"&gt;arrays&lt;/a&gt; with &lt;code&gt;each&lt;/code&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;).each {|i| puts i }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
But for simple integer loops like this, we also have &lt;code&gt;upto&lt;/code&gt;&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;) {|i| puts i }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
 and &lt;code&gt;times&lt;/code&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;.times {|i| puts i+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
Here's where I should've stopped but I can't help myself, I just have to show off with some &lt;a href="http://weblog.raganwald.com/2008/02/1100inject.html"&gt;Symbol#to_proc&lt;/a&gt; "magic".&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:p&lt;/span&gt;)&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
The above works because &lt;code&gt;p&lt;/code&gt; is an alias of &lt;code&gt;puts&lt;/code&gt; and &lt;code&gt;&amp;amp;&lt;/code&gt; converts the symbol &lt;code&gt;:p&lt;/code&gt; to a proc that is called with the numbers in the range as parameters.&lt;/p&gt;

&lt;p&gt;The alias &lt;code&gt;p&lt;/code&gt; also gives us, what I think has to be, the shortest possible way.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;p *&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;p&gt;You could argue that it's a bad thing that there are so many ways to do something as simple as this. But I see no big problem here, if any at all, even though these are hardly all possible ways to loop over integers in Ruby.&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jun 2010 21:56:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:f6e3d9ac-2271-4371-a87d-5a89303735e6</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2010/06/21/a-simple-loop</link>
      <category>Ruby</category>
    </item>
    <item>
      <title>String concatenation in Ruby</title>
      <description>&lt;p&gt;&lt;img src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Knit-schematic.png/200px-Knit-schematic.png" style="display: inline-block; float:right"/&gt;
There's no &lt;code&gt;StringBuilder&lt;/code&gt; class in Ruby because the &lt;a href="http://ruby-doc.org/core/classes/String.html"&gt;String&lt;/a&gt; class has the &lt;a href="http://ruby-doc.org/core/classes/String.html#M000807"&gt;&amp;lt;&amp;lt;&lt;/a&gt; for appending. The problem is that not every Ruby programmer seems to be aware of it. Recently I've seen &lt;code&gt;+=&lt;/code&gt; being used to append to strings where &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; would have been a much better choice.&lt;/p&gt;

&lt;p&gt;The problem with using &lt;code&gt;+=&lt;/code&gt; is that it creates a new String instance and if you do that in a loop you can get really horrible performance.&lt;/p&gt;

&lt;p&gt;If you are dealing with an array you don't even have to use &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; because &lt;a href="http://ruby-doc.org/core/classes/Array.html#M002182"&gt;Array#join&lt;/a&gt; is even faster and shows intent in a nice way.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;require &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;benchmark&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;array_of_rnd_strings=(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;...&lt;span style="color:#00D;font-weight:bold"&gt;262144&lt;/span&gt;).map{&lt;span style="color:#00D;font-weight:bold"&gt;65&lt;/span&gt;.+(rand(&lt;span style="color:#00D;font-weight:bold"&gt;25&lt;/span&gt;)).chr}&lt;tt&gt;
&lt;/tt&gt;                                 .join.scan(&lt;span style="background-color:#fff0ff"&gt;&lt;span style="color:#404"&gt;/&lt;/span&gt;&lt;span style="color:#808"&gt;.{1,8}&lt;/span&gt;&lt;span style="color:#404"&gt;/&lt;/span&gt;&lt;span style="color:#C2C"&gt;m&lt;/span&gt;&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#036;font-weight:bold"&gt;Benchmark&lt;/span&gt;.bm &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt; |benchmark|&lt;tt&gt;
&lt;/tt&gt;  benchmark.report &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    str=array_of_rnd_strings.join&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  benchmark.report &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    str2=&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    array_of_rnd_strings.each &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt; |s|&lt;tt&gt;
&lt;/tt&gt;      str2&amp;lt;&amp;lt;s&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  benchmark.report &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    str3=&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    array_of_rnd_strings.each &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt; |s|&lt;tt&gt;
&lt;/tt&gt;      str3+=s&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;


The array_of_rnd_strings is an array of 32768 8 characters long random strings.
&lt;br/&gt;
&lt;br/&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;td&gt;&lt;b&gt;user&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;system&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;total&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;real&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;    
&lt;td&gt;0.030000&lt;/td&gt;&lt;td&gt;0.000000&lt;/td&gt;&lt;td&gt;0.030000&lt;/td&gt;&lt;td&gt;(  0.027184)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;0.160000&lt;/td&gt;&lt;td&gt;0.010000&lt;/td&gt;&lt;td&gt;0.170000&lt;/td&gt;&lt;td&gt;(  0.190277)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;106.020000&lt;/td&gt;&lt;td&gt;0.300000&lt;/td&gt;&lt;td&gt;106.320000&lt;/td&gt;&lt;td&gt;(113.457793)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;p&gt;The performance of &lt;code&gt;+=&lt;/code&gt; was even &lt;strong&gt;worse&lt;/strong&gt; than I imagined!&lt;/p&gt;</description>
      <pubDate>Tue, 02 Feb 2010 00:04:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:08c4064d-9cef-4bee-8988-ea89961d87a2</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2010/02/02/string-concatenation-in-ruby</link>
      <category>Ruby</category>
    </item>
    <item>
      <title>Comparing instance variables in Ruby</title>
      <description>&lt;p&gt;Say you have two objects of the same class and you want to know what differs between them. Well actually you just want to know the instance variables in object &lt;em&gt;&lt;strong&gt;b&lt;/strong&gt;&lt;/em&gt; that differs from the ones in object &lt;em&gt;&lt;strong&gt;a&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To begin with, we need a class. I like cheese.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;class&lt;/span&gt; &lt;span style="color:#B06;font-weight:bold"&gt;Cheese&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  attr_accessor &lt;span style="color:#A60"&gt;:name&lt;/span&gt;, &lt;span style="color:#A60"&gt;:weight&lt;/span&gt;, &lt;span style="color:#A60"&gt;:expire_date&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;initialize&lt;/span&gt;(name, weight, expire_date)&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@name&lt;/span&gt;, &lt;span style="color:#33B"&gt;@weight&lt;/span&gt;, &lt;span style="color:#33B"&gt;@expire_date&lt;/span&gt; = name, weight, expire_date&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Then we need some &lt;strike&gt;cheese&lt;/strike&gt; objects.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;stilton=&lt;span style="color:#036;font-weight:bold"&gt;Cheese&lt;/span&gt;.new(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;Stilton&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;250&lt;/span&gt;, &lt;span style="color:#036;font-weight:bold"&gt;Date&lt;/span&gt;.parse(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;2009-11-02&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;gorgonzola=&lt;span style="color:#036;font-weight:bold"&gt;Cheese&lt;/span&gt;.new(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;Gorgonzola&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;250&lt;/span&gt;, &lt;span style="color:#036;font-weight:bold"&gt;Date&lt;/span&gt;.parse(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;2009-11-17&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;))&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;With only &lt;em&gt;name&lt;/em&gt;, &lt;em&gt;weight&lt;/em&gt; and an &lt;em&gt;expiration date&lt;/em&gt; it would be easy to compare those but imagine that these two objects has 42 properties. It does not stop there, you are being asked to compare 24 different classes in this way. Are you cringing yet?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Object#instance_variables&lt;/code&gt; to the rescue! Well, that and a small hack by me. Below I add a new method called &lt;code&gt;instance_variables_compare&lt;/code&gt; to &lt;code&gt;Object&lt;/code&gt;. The long method name is because I wanted to follow the naming already in place. Usually I prefer to do these kind of things as a &lt;code&gt;module&lt;/code&gt; and then &lt;code&gt;include&lt;/code&gt; them where appropriate but in this case I find that a &lt;a href="http://en.wikipedia.org/wiki/Monkey_patch"&gt;monkey patch&lt;/a&gt; will do.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;class&lt;/span&gt; &lt;span style="color:#B06;font-weight:bold"&gt;Object&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;instance_variables_compare&lt;/span&gt;(o)&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#036;font-weight:bold"&gt;Hash&lt;/span&gt;[*&lt;span style="color:#038;font-weight:bold"&gt;self&lt;/span&gt;.instance_variables.map {|v|&lt;tt&gt;
&lt;/tt&gt;      &lt;span style="color:#038;font-weight:bold"&gt;self&lt;/span&gt;.instance_variable_get(v)!=o.instance_variable_get(v) ? &lt;tt&gt;
&lt;/tt&gt;      [v,o.instance_variable_get(v)] : []}.flatten]&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;It returns the instance variables that differs as a &lt;a href="http://ruby-doc.org/core/classes/Hash.html"&gt;hash&lt;/a&gt; because it's handy and because I like it that way.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&amp;gt;&amp;gt; stilton.instance_variables_compare(gorgonzola)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@name&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Gorgonzola&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@expire_date&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="color:#666"&gt;#&amp;lt;Date: 4910305/2,0,2299161&amp;gt;}&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; gorgonzola.instance_variables_compare(stilton)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@name&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Stilton&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@expire_date&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="color:#666"&gt;#&amp;lt;Date: 4910275/2,0,2299161&amp;gt;}&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; stilton.expire_date=gorgonzola.expire_date&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#666"&gt;#&amp;lt;Date: 4910305/2,0,2299161&amp;gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; stilton.instance_variables_compare(gorgonzola)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@name&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Gorgonzola&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;}&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; stilton.instance_variables_compare(stilton)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;If you ever think of using this code you should be aware of two things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This code is very untested and comes with no guarantees.&lt;/li&gt;
&lt;li&gt;Since instance variables &lt;strong&gt;spring into life the first time they are assigned to&lt;/strong&gt; you either have to work with objects that always initialize everything or you have to change &lt;code&gt;instance_variables_compare&lt;/code&gt; to handle this.&lt;/li&gt;
&lt;/ol&gt;</description>
      <pubDate>Mon, 02 Nov 2009 22:50:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:655f1e0f-877a-4696-8057-8efd315ed457</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2009/11/02/comparing-instance-variables-in-ruby</link>
      <category>Ruby</category>
    </item>
    <item>
      <title>Infinite ranges in C#</title>
      <description>&lt;p&gt;I &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/10/15/as-timeless-as-infinity.aspx"&gt;recently learned&lt;/a&gt; that C# is compliant with the &lt;a href="http://en.wikipedia.org/wiki/IEEE_754-1985"&gt;IEEE 754-1985&lt;/a&gt; for floating point arithmetics. That wasn't a big surprise but that &lt;a href="http://en.wikipedia.org/wiki/Division_by_zero#In_computer_arithmetic"&gt;division by zero&lt;/a&gt; is defined as &lt;code&gt;Infinity&lt;/code&gt; in it was! It actually kind of bothers me that I didn't know this.&lt;/p&gt;

&lt;p&gt;In mathematics division by zero is &lt;a href="http://en.wikipedia.org/wiki/Defined_and_undefined"&gt;undefined&lt;/a&gt; for real numbers but I guess &lt;code&gt;Infinity&lt;/code&gt; is a more pragmatic result. Or as a friend put it &lt;em&gt;"IEEE stands for Institute of Electrical and Electronics Engineers not Institute of Mathematics"&lt;/em&gt;&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;n = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;636413622384679305&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;  System.Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Yes it certainly is!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
This C# code does not throw an exception, it simply leaves n defined as Infinity and writes a line to the console.&lt;/p&gt;

&lt;p&gt;Ruby is &lt;a href="http://weblog.jamisbuck.org/2007/2/7/infinity"&gt;also&lt;/a&gt; IEEE 754-1985 compliant. It even lets you define &lt;a href="http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/"&gt;infinite ranges&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;=&lt;span style="color:#60E;font-weight:bold"&gt;1.0&lt;/span&gt;/&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt;&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).include?(&lt;span style="color:#00D;font-weight:bold"&gt;162259276829213363391578010288127&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).step(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;).take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:+&lt;/span&gt;) &lt;span style="color:#666"&gt;# 7+14+21&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;42&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
I can't say I see very much &lt;a href="http://www.michaelharrison.ws/weblog/?p=163"&gt;use&lt;/a&gt; of this but it brings a kind of completeness to the handling of infinities. Unfortunately it seems we don't get that in C# out of the box because &lt;code&gt;Enumerable.Range&lt;/code&gt; takes &lt;code&gt;&amp;lt;int&amp;gt;,&amp;lt;int&amp;gt;&lt;/code&gt; as parameters and there's no &lt;code&gt;Infinity&lt;/code&gt; definition for &lt;code&gt;int&lt;/code&gt;.  That's unless someone wrote a generic Range class. Turns out &lt;a href="http://stackoverflow.com/users/22656/jon-skeet"&gt;none other&lt;/a&gt; than &lt;a href="http://www.yoda.arachsys.com/csharp/"&gt;Jon Skeet&lt;/a&gt; did in his &lt;a href="http://www.yoda.arachsys.com/csharp/miscutil/"&gt;MiscUtil&lt;/a&gt;. Download MiscUtil and then by &lt;code&gt;using MiscUtil.Collections;&lt;/code&gt; you can:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; infinity = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, infinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (r.Contains(&lt;span style="color:#00D;font-weight:bold"&gt;4711&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;  System.Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Yes it certainly does!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
And guess what, it works like a charm! &lt;code&gt;4711&lt;/code&gt; is part of positive infinity and &lt;code&gt;sum&lt;/code&gt; is 42.0 and all is good.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;There's also a couple of predefined constants. Thanks to Eric for pointing that out.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;,  System.Double.PositiveInfinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 20 Oct 2009 20:41:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:1337fd12-5058-4b35-bbf4-c9d0c4e9ade8</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2009/10/20/infinite-ranges-in-c</link>
      <category>Ruby</category>
      <category>C#</category>
      <category>Math</category>
    </item>
    <item>
      <title>The Thrush combinator in C#</title>
      <description>&lt;p&gt;Last year I read &lt;a href="http://github.com/raganwald/homoiconic/blob/master/README.markdown"&gt;Reg "Raganwald'" Braithwaite's&lt;/a&gt; excellent post &lt;a href="http://github.com/raganwald/homoiconic/blob/master/2008-10-30/thrush.markdown#readme"&gt;The Thrush&lt;/a&gt; and he  explains it as &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;The thrush is written Txy = yx. It reverses evaluation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Back then I didn't even consider trying to implement it in C#. That was before I digged deeper into &lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;lambda expressions&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension methods&lt;/a&gt; in C# 3.0 and way before last night when I read Debasish Ghosh's post on how to &lt;a href="http://debasishg.blogspot.com/2009/09/thrush-combinator-in-scala.html"&gt;implement the Thrush in Scala&lt;/a&gt;. After reading that my first thought was if it was possible to do the same in C#. Here's my attempt.&lt;/p&gt;

&lt;p&gt;At first I struggled with the static typing and headed for an easy way out using Object in the extension method of Object:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; object Into(this Object obj, &lt;tt&gt;
&lt;/tt&gt;                        Func&amp;lt;object, object&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f.Invoke(obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
My goal was to translate the Ruby example&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).select(&amp;amp;&lt;span style="color:#A60"&gt;:odd?&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:+&lt;/span&gt;).into { |x| x * x }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
in Raganwald's post to C#.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;Which reads "Take the numbers from 1 to 100, keep the odd ones, take the sum of those, and then answer the square of that number."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But with the Object based extension method I had to do some ugly casts.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Where(x =&amp;gt; Odd(x)).Sum().Into(x =&amp;gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)x * (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
With som added typing I could do: &lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; result = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Where(x =&amp;gt; Odd(x)).Sum().Into(x =&amp;gt; x * x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
That merely moved the cast to the extension method and also made it work for integers only.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; Into(this Object obj, Func&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;, &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f.Invoke((&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
Then I remembered generics and method type inference which finally led to a decent Thrush combinator in C#.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; T Into&amp;lt;T&amp;gt;(this T obj, Func&amp;lt;T, T&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f(obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
The casts are gone and it's also, as far as I can see, as flexible as the one in Ruby.&lt;br&gt;&lt;br&gt;
Contrived example follows:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; test = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;ball&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; ball = test.Into(s =&amp;gt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Are we having a &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + s + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; yet?&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
&lt;strong&gt;The odd part&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Odd(x)&lt;/em&gt; method call in the calculation above is a plain static method.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;private &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; Odd(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; (n % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
If you want an even more terse syntax you could try an ext. method on IEnumerable like this:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; Odd(this IEnumerable&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; en)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; en.Where(n =&amp;gt; n % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
Gives:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; result = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Odd().Sum().Into(x =&amp;gt; x * x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
&lt;!--Also as a general alternative to &lt;code&gt;.Sum()&lt;/code&gt; I could have used &lt;code&gt;.Aggregate((x, y) =&amp;gt; x + y))&lt;/code&gt; but I found it a bit verbose.--&gt;
In C# I don't think it's possible to pull off the Symbol#to_proc stuff that Ruby does. That's the &lt;em&gt;&amp;amp;:&lt;/em&gt; in the &lt;em&gt;select(&amp;amp;:odd?)&lt;/em&gt; and the &lt;em&gt;inject(&amp;amp;:+)&lt;/em&gt; in the Ruby example. Raganwald has a great &lt;a href="http://weblog.raganwald.com/2007/11/fun-with-symboltoproc.html"&gt;post&lt;/a&gt; on that too.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Check out &lt;a href="http://stackoverflow.com/questions/1528319/operators-as-method-parameters-in-c-and-the-thrush-combinator"&gt;Jon Skeet's nice answer&lt;/a&gt; on StackOverflow to my question on how to make this even more Ruby-like. I have to try out that Operator class later though.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit 2009-10-07&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;One thing I found a bit surprising is that by implementing the Into ext. method in this way it not only works for all objects based on &lt;code&gt;System.Object&lt;/code&gt; but it also works  for &lt;a href="http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx"&gt;value types&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n=&lt;span style="color:#00D;font-weight:bold"&gt;4711&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; oddOrZero = n.Into(x =&amp;gt; x % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; !=&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; ? x : &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); &lt;span style="color:#666"&gt;// 4711&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;n = &lt;span style="color:#00D;font-weight:bold"&gt;4712&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;oddOrZero = n.Into(x =&amp;gt; x % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; ? x : &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); &lt;span style="color:#666"&gt;// 0&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
&lt;font style="color:red;font-weight:bold"&gt;Edit 2009-10-12&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;My confusion did stem from my lack of understanding of &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977(loband).aspx"&gt;extension methods&lt;/a&gt;. Ex. methods are in fact not extending &lt;code&gt;System.Object&lt;/code&gt; or any other type, they are "&lt;a href="http://blogs.msdn.com/ericlippert/about.aspx"&gt;nothing more than a pleasant syntax for calling a static method&lt;/a&gt;" in  case no instance method with the same name can be found.&lt;/p&gt;</description>
      <pubDate>Tue, 06 Oct 2009 20:35:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b02ff8f0-44fb-44ca-a309-eb0fd074a495</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2009/10/06/the-thrush-combinator-in-c</link>
      <category>Ruby</category>
      <category>C#</category>
    </item>
    <item>
      <title>A case for using only three different digits in keypad codes</title>
      <description>&lt;p&gt;Keypads have obvious security problems and keypads accepting a stream of digits with no # or enter in between, while checking for the four digit long code, are even worse.&lt;/p&gt;

&lt;p&gt;The important part is to not leak the digits in the code &lt;a href="http://www.schneier.com/blog/archives/2009/07/information_lea_1.html"&gt;by wear&lt;/a&gt; or intentional markings because if they leak it's suddenly very far from 10000 combinations.&lt;/p&gt;

&lt;p&gt;If the "lock picker" only knows that the code contains four digits there are 10000 combinations. Keypads accepting a stream of digits can then be opened in a maximum of 10003 keystrokes using the &lt;a href="http://en.wikipedia.org/wiki/De_Bruijn_sequence#Uses"&gt;De
Bruijn sequence&lt;/a&gt;. That is still quite a lot.&lt;/p&gt;

&lt;p&gt;Below is a Ruby implementation of the &lt;a href="http://www.hakank.org/comb/debruijn.cgi"&gt;De
Bruijn sequence&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#666"&gt;# De Bruijn sequence&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#666"&gt;# Original implementation by Frank Ruskey (1994)&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#666"&gt;# translated to C by Joe Sawada&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#666"&gt;# translated to Ruby by Jonas Elfström (2009)&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@n&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@k&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@a&lt;/span&gt;=[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@sequence&lt;/span&gt;=[]&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;debruijn&lt;/span&gt;(t, p)&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; t&amp;gt;&lt;span style="color:#33B"&gt;@n&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; &lt;span style="color:#33B"&gt;@n&lt;/span&gt;%p==&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;      &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(p) {|j| &lt;span style="color:#33B"&gt;@sequence&lt;/span&gt;&amp;lt;&amp;lt;&lt;span style="color:#33B"&gt;@a&lt;/span&gt;[j]}&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t]=&lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t-p]&lt;tt&gt;
&lt;/tt&gt;    debruijn(t+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,p)&lt;tt&gt;
&lt;/tt&gt;    (&lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t-p]+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;).upto(&lt;span style="color:#33B"&gt;@k&lt;/span&gt;-&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;) {|j|&lt;tt&gt;
&lt;/tt&gt;      &lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t]=j&lt;tt&gt;
&lt;/tt&gt;      debruijn(t+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,t)&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;debruijn(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;print &lt;span style="color:#33B"&gt;@sequence&lt;/span&gt;.join&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
It's not uncommon to find keypads with 4 of the 10 keys worn down and if you do you can be pretty sure that the code contains those four different digits. The number of possible combinations are 4! = 4x3x2x1 = 24. I got curious to see if there's a kind of &lt;a href="http://mathworld.wolfram.com/deBruijnSequence.html"&gt;De Bruijn sequence&lt;/a&gt; for this that brings down the 4*24=96 keystrokes. By scribbling in a &lt;a href="http://www.scintilla.org/SciTE.html"&gt;text editor&lt;/a&gt; I quickly realized there's not a clean sequence. Not clean in the way that a sequence following the rules can be created. Also it's probably even quite daunting to present it as mathematically dense and beautiful as the &lt;a href="http://www.stefangeens.com/2004/10/the-de-bruijn-c.html"&gt;De Bruijn&lt;/a&gt; but that could be my less than great &lt;a href="http://en.wikipedia.org/wiki/Combinatorics"&gt;combinatorics&lt;/a&gt; speaking.&lt;/p&gt;

&lt;p&gt;I made a quick and dirty brute force hack to try to find a shorter sequence.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;seq=[]&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|a| &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|b| &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|c| &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|d|&lt;tt&gt;
&lt;/tt&gt;  seq &amp;lt;&amp;lt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;%d%d%d%d&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; % [a,b,c,d] &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; !(a==b || a==c || a==d || b==c || b==d || c==d)&lt;tt&gt;
&lt;/tt&gt;}}}}&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;s=seq[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;seq.delete_at(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;while&lt;/span&gt; (seq.length&amp;gt;&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;  next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-3&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; next_code.empty?&lt;tt&gt;
&lt;/tt&gt;    next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-2&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; next_code.empty? &lt;tt&gt;
&lt;/tt&gt;      next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;      s+=next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;][&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;      seq.delete_at(seq.index(next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]))&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;      s+=next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;][&lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;      seq.delete_at(seq.index(next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]))&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-3&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;    s+=next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;][&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;].chr&lt;tt&gt;
&lt;/tt&gt;    seq.delete_at(seq.index(next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]))&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
The above code takes the first code "1234" of the 24 and then searches the rest of the array for a code beginning with "234". It finds "2341" and adds "1" to the end of &lt;em&gt;s&lt;/em&gt; and continues to look for "341" and so on. Relatively soon there is no three digit match and then it tries two digits and eventually even that fails and then it gets the first one digit match. The resulting sequence is: &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;123412314231243121342132413214321&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From 96 to 33 keystrokes. Not as effective as De Bruijn but still significant. Unlike De Bruijn I have absolutely no proof that this is the shortest one possible but it seems likely. Also notice that in the middle of the sequence we find "3121" and "1213". Those break the criteria of four different digits but they seem to be necessary to be able to enter the reversed mode. Try reading the sequence forward and backwards to see what I mean.&lt;/p&gt;

&lt;p&gt;If the code only contains two digits it's gets even more trivial to try them all. There are 14 possible codes and by &lt;em&gt;compressing&lt;/em&gt; those to one sequence you get down to 20 keystrokes. &lt;/p&gt;

&lt;p&gt;Things get a little more interesting if only three buttons are worn. It turns out that the repeated digits can be placed in the code in six different ways.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;0012,1002,1200,0102,0120,1020&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's 6x2x3=36 combinations and, maybe a little unintuitive, 12 more than if you are using four different digits. I compressed it down to 49 key strokes (16 more than with four different digits). Unlike the sequence for four different digits I can't find it with google and I know it's kind of security by obscurity but I still chose not to publish it here.&lt;/p&gt;

&lt;p&gt;Be aware that if an attacker knows you are using a 0012-like code he gets a smaller space to search. 6x8x9x10=4320 instead of 10000. You have to weight the risk of button leaks against a code protocol leak.
&lt;br&gt;&lt;br&gt;
&lt;font color="red"&gt;Edit 2010-10-25&lt;/font&gt;&lt;br&gt;
&lt;a href="http://stackoverflow.com/questions/4008603/how-to-compute-de-bruijn-sequences-for-non-power-of-two-sized-alphabets/4009417#4009417"&gt;Uckelman noticed&lt;/a&gt; that the &lt;code&gt;alike&lt;/code&gt; variable in the former version of the debruijn-script wasn't used so I removed it.&lt;/p&gt;</description>
      <pubDate>Sun, 27 Sep 2009 21:02:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:9e10d05b-1a1d-46a1-82da-0ba366d32035</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2009/09/27/a-case-for-using-only-three-different-digits-in-keypad-codes</link>
      <category>Security</category>
      <category>Ruby</category>
    </item>
  </channel>
</rss>

