<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>David Vollbracht - Home</title>
  <id>tag:davidvollbracht.com,2010:mephisto/</id>
  <generator version="0.7.3" uri="http://mephistoblog.com">Mephisto Noh-Varr</generator>
  <link href="http://davidvollbracht.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://davidvollbracht.com/" rel="alternate" type="text/html"/>
  <updated>2010-03-09T14:31:29Z</updated>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2010-03-09:54</id>
    <published>2010-03-09T14:29:00Z</published>
    <updated>2010-03-09T14:31:29Z</updated>
    <link href="http://davidvollbracht.com/2010/3/9/next-afpug-meeting-is-april-6th" rel="alternate" type="text/html"/>
    <title>Next AFPUG Meeting is April 6th</title>
<content type="html">
            &lt;p&gt;The next meeting of the Atlanta Function Programming Users Group is going to be Monday, April 6th.  All the details are available &lt;a href='http://groups.google.com/group/AFPUG'&gt;here&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;As I mentioned in my last post, the agenda is to compare implementations of the first 10 &lt;a href='http://projecteuler.net'&gt;Project Euler&lt;/a&gt; problems in different functional languages.  I look forward to seeing everyone&#8217;s problem solutions!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2010-03-02:53</id>
    <published>2010-03-02T16:36:00Z</published>
    <updated>2010-03-02T16:37:37Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2010/3/2/first-afpug-meeting" rel="alternate" type="text/html"/>
    <title>First AFPUG Meeting!</title>
<content type="html">
            &lt;p&gt;Last night we held the first Atlanta Functional Programming Users Group meeting at the ThoughtWorks office in Atlanta.  I gave a short introduction Haskell, which generated a lot of good questions.  You can check out the presentation at the group&#8217;s slideshare &lt;a href='http://www.slideshare.net/group/atlanta-functional-programming-users-group'&gt;page&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;While the group&#8217;s next meetup date wasn&#8217;t specifically decided, it should sometime in April.  The agenda for the next meeting is to compare solutions to the first 10 &lt;a href='http://projecteuler.net/'&gt;Project Euler&lt;/a&gt; problems in multiple functional languages.  Our door is always open to new members, so come and join us!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2010-02-19:51</id>
    <published>2010-02-19T23:10:00Z</published>
    <updated>2010-02-19T23:46:44Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2010/2/19/the-dark-side-of-elastic-ips" rel="alternate" type="text/html"/>
    <title>The Dark Side of Elastic IPs</title>
<content type="html">
            &lt;h2&gt;Did I inherit thothle.ca?&lt;/h2&gt;


	&lt;p&gt;Google &#8220;David Vollbracht&#8221;.  What domain name comes up?  Right now the domain is thothle.ca.  In fact, you may even be viewing this page on thothle.ca.   This is quite awkward, as I do &lt;span class='caps'&gt;NOT&lt;/span&gt; own thothle.ca.  I&#8217;m not even Canadian.  Nonetheless, I&#8217;m writing this post right now on thothle.ca, because it seemed appropriate.&lt;/p&gt;


	&lt;h2&gt;whois thothle.ca&lt;/h2&gt;


	&lt;p&gt;No, I won&#8217;t post all the contact info here, but suffice to say the nameservers for thothle.ca are from mydyndns.org.  My conclusion:  the owner of thothcle.ca was using my Amazon elastic IP and released it.  He did not, however, update his dns to reflect that the IP is no longer his.  Consequently his domain now points to my website, and has become the #1 page on google when searching for my name.&lt;/p&gt;


	&lt;h2&gt;Conclusion&lt;/h2&gt;


	&lt;p&gt;Obviously this isn&#8217;t really Amazon&#8217;s fault. They can&#8217;t exactly follow up after every IP is released and check where it is pointing now.  The best I can do is contact the domain&#8217;s owner and hope he&#8217;ll change the nameserver config to not point to the IP.  Funnily enough, this kind of reminds me of the poor chap who has my old cell phone number&#8230; I can empathize now.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2010-02-16:50</id>
    <published>2010-02-16T11:38:00Z</published>
    <updated>2010-02-16T11:39:59Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2010/2/16/come-to-the-first-afpug-meeting" rel="alternate" type="text/html"/>
    <title>Come to the first AFPUG Meeting!</title>
<content type="html">
            &lt;p&gt;I&#8217;m pleased to announce that ThoughtWorks is sponsoring the first Atlanta Function Programming Users Group meeting.  All the info is &lt;a href='http://groups.google.com/group/AFPUG/web/our-first-meet-up'&gt;here at our google group page&lt;/a&gt;.   I&#8217;ll be giving a short introduction to the Haskell programming language and then we&#8217;ll open up the floor to discussion about the group&#8217;s future.  If you&#8217;re in Atlanta (or can drive there) and you&#8217;re interested in &lt;a href='http://en.wikipedia.org/wiki/Functional_programming'&gt;Functional Programming&lt;/a&gt;, this is the place for you!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2010-02-03:49</id>
    <published>2010-02-03T16:23:00Z</published>
    <updated>2010-02-03T16:25:55Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2010/2/3/now-hosted-on-ec2" rel="alternate" type="text/html"/>
    <title>Now Hosted on EC2</title>
<content type="html">
            &lt;p&gt;I&#8217;ve moved the hosting for this blog from my old &lt;span class='caps'&gt;VPS&lt;/span&gt; into Amazon &lt;span class='caps'&gt;EC2&lt;/span&gt;.  My calculations indicate this should save me around $9-$10 a month when using a single reserved m1.small instance.  This is a pretty good deal since an m1.small instance has over twice as much memory and about 60% more cpu horsepower.  The &lt;span class='caps'&gt;EC2&lt;/span&gt; setup isn&#8217;t very complicated, so I&#8217;ll give you an overview right here.&lt;/p&gt;


	&lt;p&gt;I&#8217;m running a single elastic ip attached to a single running m1.small instance.  I started with the public Fedora Ruby on Rails Web Starter &lt;span class='caps'&gt;AMI&lt;/span&gt; (ami-22b0534b) and lightly customized it to fit my needs.  That included installing a few rubygems and mucking about in init.d and chkconfig to get the services starting the way I wanted.&lt;/p&gt;


	&lt;p&gt;The most complicated part of the setup was separating out some parts of the machine to an &lt;span class='caps'&gt;EBS&lt;/span&gt; volume for persistent storage.  In particular, the mysql database needs to be on persistent storage so it is safe if the instance goes down.  Also, I wanted any rails apps to be on persistent storage so I don&#8217;t have to create a new ami for every single configure change, upgrade, or deployment.  All this is achieved simply by putting symlinks to the normal places where these would live in the filesystem and pointing them to the mount point for the &lt;span class='caps'&gt;EBS&lt;/span&gt; volume.&lt;/p&gt;


	&lt;p&gt;With that done and working the only remaining issue was to ensure that the &lt;span class='caps'&gt;EBS&lt;/span&gt; volume gets attached and mounted.  A bit of googling confirmed that creating an init.d script was the generally accepted approach for this, and I found a workable starting point &lt;a href='http://codeshepherd.blogspot.com/2009/05/auto-mounting-ebs-on-ec2.html'&gt;here&lt;/a&gt;.  That solution relied on the &lt;span class='caps'&gt;EC2&lt;/span&gt; api tools, which in turn rely on Java which I didn&#8217;t relish the idea of installing.  Instead I installed the very nice &lt;a href='http://amazon-ec2.rubyforge.org/'&gt;amazon-ec&lt;/a&gt; rubygem and rewrote the mountec2vol script on top of it.  Voila!  Now bringing up a new instance attaches the &lt;span class='caps'&gt;EBS&lt;/span&gt;, mounts it, and then starts any services that needed the volume once it is available.&lt;/p&gt;


	&lt;p&gt;If you&#8217;re interested in the init.d script, you can get it &lt;a href='http://davidvollbracht.com/assets/2010/2/3/mountec2vol-rb'&gt;here&lt;/a&gt;.  It&#8217;s limited currently by having the volume id specified in the script, so you have to rebuild the ami to change the volume id.  I&#8217;ll probably extract that in the future, but it&#8217;s doing what I need for now.  If you find it useful, let me know!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2009-07-09:48</id>
    <published>2009-07-09T03:13:00Z</published>
    <updated>2009-07-09T03:16:32Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2009/7/9/dynamicteardown" rel="alternate" type="text/html"/>
    <title>DynamicTeardown</title>
<content type="html">
            &lt;p&gt;I ran into a situation while working on DeepTest recently where I wanted an object to start a temporary drb server in the middle of the test and have it be stopped automatically at the end of the test.  Ideally, I wanted to encapsulate inside the class itself, which is a fake used for testing and so can be aware of the test framework.  Here&#8217;s the snippet I ended up with:&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
module DynamicTeardown
  class &amp;lt;&amp;lt;self
    def dynamic_teardowns
      @dynamic_teardowns ||= []
    end

    def on_teardown(&#38;block)
      dynamic_teardowns &amp;lt;&amp;lt; block
    end

    def run_dynamic_teardowns
      while td = dynamic_teardowns.shift
        td.call rescue nil
      end
    end
  end
end

module DRbTestHelp
  def drb_server_for(server)
    drb_server = DRb::DRbServer.new &quot;druby://localhost:0&quot;, server
    DynamicTeardown.on_teardown { drb_server.stop_service }
    return DRbObject.new_with_uri(drb_server.uri)
  end
end

class Test::Unit::TestCase
  def teardown
    DynamicTeardown.run_dynamic_teardowns
  end
end
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;This nice little implementation allows the class to know that it needs to get some teardown done while allowing the test framework to determine when the teardowns are called.  So far it&#8217;s working great for me.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2009-06-24:47</id>
    <published>2009-06-24T10:53:00Z</published>
    <updated>2009-06-24T10:56:30Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2009/6/24/deeptest-2_0-in-the-works" rel="alternate" type="text/html"/>
    <title>DeepTest 2.0 in the works</title>
<content type="html">
            &lt;p&gt;I&#8217;ve started on a significant overhaul of DeepTest which will be released as version 2.0 when it&#8217;s finished.  I thought I&#8217;d share some of my plans for what 2.0 will include and some thoughts about what might come versions after.  I don&#8217;t have a timetable for yet, but work so far is going fairly quickly.&lt;/p&gt;


	&lt;p&gt;Plans for DeepTest 2.0&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;A revamped distributed DeepTest architecture that will help ease setup and maintenance.&lt;/li&gt;
		&lt;li&gt;A new console user interface that will provide much more information about what is going on during the test run&lt;/li&gt;
		&lt;li&gt;Much better error handling &#38; reporting (no more &#8220;Maybe an error was printed above?&#8221;!)&lt;/li&gt;
		&lt;li&gt;RSpec integration that doesn&#8217;t break with every new release of rspec.&lt;/li&gt;
		&lt;li&gt;Automatic configuration for Rails projects&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;What comes after 2.0?  The next big feature I&#8217;d like to implement is support for cloud testing with deep test on &lt;span class='caps'&gt;EC2&lt;/span&gt;.  Other than that, the features coming after 2.0 will likely be driven be requests.  Is there a feature you&#8217;d like to see in DeepTest?  Comment here or message me (qxjit) on github and we&#8217;ll get it rolling!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-07-01:42</id>
    <published>2008-07-01T02:48:00Z</published>
    <updated>2008-07-01T04:00:04Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/7/1/30-days-of-tech-day-30-haskell-decompose" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 30 - Haskell Decompose</title>
<summary type="html">&lt;p&gt;Some time ago Tom, a friend, presented a math programming challenge to a me and a few others.  The challenge was to create a function that when given a positive number would return a set of all the sets of positive numbers   whose sum was the original number.  He wasn&#8217;t so much interested in the result itself as he was in what sort of techniques were used in the implementation&#8212;memoization of course being a choice candidate for this problem.  I was toying a lot with Haskell at the time, and since this was fundamentally a math problem, Haskell seemed well suited to the problem too.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Some time ago Tom, a friend, presented a math programming challenge to a me and a few others.  The challenge was to create a function that when given a positive number would return a set of all the sets of positive numbers   whose sum was the original number.  He wasn&#8217;t so much interested in the result itself as he was in what sort of techniques were used in the implementation&#8212;memoization of course being a choice candidate for this problem.  I was toying a lot with Haskell at the time, and since this was fundamentally a math problem, Haskell seemed well suited to the problem too.&lt;/p&gt;
&lt;p&gt;Here&#8217;s what I came up with:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  module Decompose
    where

  import Data.List

  decompose = ((map decompose' [0..]) !!) where
    decompose' n = nub (map sort (decompositions n (floor ((toRational n)/2))))

  decompositions n 0 = [[n]]
  decompositions n m = nmCombinations ++ decompositions n (m - 1) where
    nmCombinations = [a ++ b | a &amp;lt;- (decompose (n - m)), b &amp;lt;- (decompose m)]
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;I was surprised at how short the Haskell solution was.  If you run this through Hugs, you&#8217;ll see something like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  Hugs&amp;gt; :load &quot;Decompose&quot; 
  Decompose&amp;gt; decompose 5
  [[1,1,1,1,1],[1,1,1,2],[1,2,2],[1,1,3],[2,3],[1,4],[5]]
  Decompose&amp;gt; decompose 10
  [[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,2],[1,1,1,1,1,1,2,2],[1,1,1,1,1,1,1,3],[1,1,1,1,1,2,3],[1,1,1,1,1,1,4],[1,1,1,1,1,5],[1,1,1,1,2,2,2],[1,1,1,2,2,3],[1,1,1,1,2,4],[1,1,1,2,5],[1,1,2,2,2,2],[1,2,2,2,3],[1,1,2,2,4],[1,2,2,5],[1,1,1,1,3,3],[1,1,2,3,3],[1,1,1,3,4],[1,1,3,5],[2,2,3,3],[1,2,3,4],[2,3,5],[1,1,4,4],[1,4,5],[5,5],[1,3,3,3],[3,3,4],[2,2,2,2,2],[2,2,2,4],[2,4,4],[1,1,1,1,6],[1,1,2,6],[2,2,6],[1,3,6],[4,6],[1,1,1,7],[1,2,7],[3,7],[1,1,8],[2,8],[1,9],[10]]
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The only performance trick in here is indeed memoization, though that may not be obvious at first.  It lies in the definition of &lt;code&gt;decompose&lt;/code&gt;.  The &lt;code&gt;decompose&lt;/code&gt; function is really just a list of decomposition results for all positive numbers, indexed by the number being decomposed.  Obviously this would be an infinitely long lost, but Haskell has no problem handling that&#8212;it just calculates elements of the list on demand.  Since decomposing a number always involves decomposing all the numbers below it, the fact that you have to calculate the list out to the length of the original number being decomposed is not a hindrance, it&#8217;s actually an advantage!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-06-30:40</id>
    <published>2008-06-30T03:07:00Z</published>
    <updated>2008-06-30T04:00:04Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/6/30/30-days-of-tech-day-29-c-ruby-threads-oh-my" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 29 - C, Ruby, &amp; Threads, Oh My!</title>
<summary type="html">&lt;p&gt;When &lt;a href='http://ph7spot.com/'&gt;Philippe&lt;/a&gt; and I worked on SystemTimer together we went back and forth about how much to write in Ruby and how much to write in C.  Writing purely at the C level gave us more control over what was happening in the Ruby process as we installed and cleared signal handlers and such, but working in Ruby produced far more readable code.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;When &lt;a href='http://ph7spot.com/'&gt;Philippe&lt;/a&gt; and I worked on SystemTimer together we went back and forth about how much to write in Ruby and how much to write in C.  Writing purely at the C level gave us more control over what was happening in the Ruby process as we installed and cleared signal handlers and such, but working in Ruby produced far more readable code.&lt;/p&gt;
&lt;p&gt;Ultimately we ended up writing a hybrid&#8212;Ruby code that calls C code that calls back into Ruby code for a few interesting bits.  When I go back and look at the result now, I&#8217;m pretty happy with it.  There was one trick we had to pull in our situation, however, to ensure everything went according to plan.  Since we entered C code to perform low level operations with timers we felt we didn&#8217;t want our code to be interrupted by Ruby&#8217;s thread scheduling.  We wanted to ensure the timer got set up properly without the chance of another thread being scheduled.  At first this led is to try to write as much in C as possible, since we wouldn&#8217;t ever hit the Ruby thread scheduler.  Eventually we realized we could clean up a bunch of C code by extracting Ruby methods.  To prevent other threads from being scheduled, we simply extracted C functions to wrap the Ruby methods, like this one.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  static void install_ruby_sigalrm_handler(VALUE self) {
    rb_thread_critical = 1;
    rb_funcall(self, rb_intern(&quot;install_ruby_sigalrm_handler&quot;), 0);
    rb_thread_critical = 0;
  }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The C function &lt;code&gt;install_ruby_sigalrm_handler&lt;/code&gt; is called by another C function, and is quite readable in that context.  If you&#8217;re reading the C code, you don&#8217;t even have to know it&#8217;s calling back to Ruby.  All the C function does, however, is set &lt;code&gt;rb_thread_critical&lt;/code&gt; and then call back into Ruby code.  &lt;code&gt;rb_thread_critical&lt;/code&gt; is the equivalent of &lt;code&gt;Thread.critical&lt;/code&gt; in the Ruby &lt;span class='caps'&gt;API&lt;/span&gt;.  When it is set, Ruby will not schedule another thread (except in a few cases documented with &lt;code&gt;Thread.critical&lt;/code&gt;).  Using this simple tool we were able to effectively blend Ruby and C code without worrying about our operations being preempted.  Arigatoo, Matsumoto-san.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-06-29:39</id>
    <published>2008-06-29T03:08:00Z</published>
    <updated>2008-06-29T04:00:04Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/6/29/30-days-of-tech-day-28-lambda-args" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 28 - lambda args</title>
<summary type="html">&lt;p&gt;The Ruby documentation for the &lt;code&gt;lambda&lt;/code&gt; method (a.k.a. &lt;code&gt;proc&lt;/code&gt;) says &#8220;Equivalent to Proc.new, except the resulting Proc objects check the number of parameters passed when called.&#8221;  This is almost exactly right.  There are, however, at least two cases where this turns out to be false.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;The Ruby documentation for the &lt;code&gt;lambda&lt;/code&gt; method (a.k.a. &lt;code&gt;proc&lt;/code&gt;) says &#8220;Equivalent to Proc.new, except the resulting Proc objects check the number of parameters passed when called.&#8221;  This is almost exactly right.  There are, however, at least two cases where this turns out to be false.&lt;/p&gt;
&lt;p&gt;The first is when the block passed to &lt;code&gt;lambda&lt;/code&gt; does not have an argument list specified.  The resulting &lt;code&gt;Proc&lt;/code&gt; object will not check arguments in this case.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  def test_lambda_checks_arguments
    l = lambda {||}
    assert_raises(ArgumentError) {l.call(1)}
  end

  def test_lambda_doesnt_check_arguments_if_no_arguments_specified
    l = lambda {}
    assert_nothing_raised {l.call(1)}
  end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The two tests above prove that &lt;code&gt;Proc#call&lt;/code&gt; doesn&#8217;t check arguments for a &lt;code&gt;lambda&lt;/code&gt; proc with no arguments &lt;strong&gt;unless you specify that the proc takes no arguments by including an empty parameter list.&lt;/strong&gt;  This is not the only case where argument checking is not done for &lt;code&gt;lambda&lt;/code&gt; procs, however.&lt;/p&gt;


	&lt;p&gt;If you pass a &lt;code&gt;lambda&lt;/code&gt; proc to a method is a block (using &lt;code&gt;&#38;&lt;/code&gt;), this circumvents the argument checking that would normally take place, even when you specify a blank argument list.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  def test_lambda_doesnt_check_arguments_when_yielded_to_even_if_arguments_specified
    l = lambda {||}
    def yield_1_to; yield 1; end
    assert_nothing_raised {yield_1_to &#38;l}
  end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The above test passes, even though one argument is yielded to a &lt;code&gt;lambda&lt;/code&gt; proc that explicitly takes no args.  I haven&#8217;t read the source yet, but my guess about what&#8217;s happening here is that Ruby converts the proc back to a normal block.  Since blocks normally don&#8217;t check arguments, there&#8217;s no reason to expect the one here to either.  Nonetheless, the result surprised me until I thought about it longer.&lt;/p&gt;


	&lt;p&gt;I categorize this as one of those Ruby oddities that for the most part doesn&#8217;t hurt you in practice.  I don&#8217;t recall ever having run into a bug or debugging issue where not knowing about this hindered me.  I do think that building knowledge about oddities like these is good practice though.  It deepens your knowledge of Ruby in general, and every so often one small bit of knowledge will save you a chunk of time.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-06-28:38</id>
    <published>2008-06-28T03:30:00Z</published>
    <updated>2008-06-28T04:31:16Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/6/28/30-days-of-tech-day-27-short-circuited-timeout" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 27 - Short Circuited Timeout</title>
<summary type="html">&lt;p&gt;Ruby&#8217;s &lt;code&gt;timeout&lt;/code&gt; method operates by raising a exception, &lt;code&gt;Timeout::Error&lt;/code&gt;.  &lt;code&gt;Timeout::Error&lt;/code&gt; inherits from &lt;code&gt;Interrupt&lt;/code&gt;, &lt;code&gt;SignalException&lt;/code&gt;, and finally &lt;code&gt;Exception&lt;/code&gt;.  In particular, it doesn&#8217;t inherit from &lt;code&gt;StandardError&lt;/code&gt;, which means it isn&#8217;t caught by a default &lt;code&gt;rescue&lt;/code&gt; clause with no exception class specified.  When I first encountered this, I was annoyed that I had to explicitly catch timeout errors anytime I wanted to recover from errors in general, whether they were timeout errors or not.  I realized something the other day that changed my opinion on this however.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Ruby&#8217;s &lt;code&gt;timeout&lt;/code&gt; method operates by raising a exception, &lt;code&gt;Timeout::Error&lt;/code&gt;.  &lt;code&gt;Timeout::Error&lt;/code&gt; inherits from &lt;code&gt;Interrupt&lt;/code&gt;, &lt;code&gt;SignalException&lt;/code&gt;, and finally &lt;code&gt;Exception&lt;/code&gt;.  In particular, it doesn&#8217;t inherit from &lt;code&gt;StandardError&lt;/code&gt;, which means it isn&#8217;t caught by a default &lt;code&gt;rescue&lt;/code&gt; clause with no exception class specified.  When I first encountered this, I was annoyed that I had to explicitly catch timeout errors anytime I wanted to recover from errors in general, whether they were timeout errors or not.  I realized something the other day that changed my opinion on this however.&lt;/p&gt;
&lt;p&gt;Here&#8217;s the &lt;code&gt;timeout&lt;/code&gt; method for Ruby 1.8.6.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  def timeout(sec, exception=Error)
    return yield if sec == nil or sec.zero?
    raise ThreadError, &quot;timeout within critical session&quot; if Thread.critical
    begin
      x = Thread.current
      y = Thread.start {
        sleep sec
        x.raise exception, &quot;execution expired&quot; if x.alive?
      }
      yield sec
      #    return true
    ensure
      y.kill if y and y.alive?
    end
  end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;It operates by starting a thread, &lt;code&gt;y&lt;/code&gt; that will raise an exception after sleeping.  It raises the exception by calling &lt;code&gt;x.raise&lt;/code&gt;, causing the exception be raised in thread &lt;code&gt;x&lt;/code&gt;, which is the thread that called &lt;code&gt;timeout&lt;/code&gt;&#8212;the one doing the work that needs to be timed out.  That exception terminates the execution of the thread wherever it happens to be and starts propagating an exception up the stack.  This exception is subject to the same handling as every other exception that might happen.  It can caught be a rescue clause.  &lt;strong&gt;In particular, it can be caught by a rescue clause that is within the block being subjected to the timeout&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  require 'timeout'

  begin
    Timeout.timeout(1) do
      begin
        sleep 2
      rescue Exception =&amp;gt; e
        puts &quot;Caught #{e.inspect} within timeout&quot; 
      end
    end
  rescue Exception =&amp;gt; e
    puts &quot;Caught #{e.inspect} at top level&quot; 
  end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This produces the output &lt;code&gt;Caught #&amp;lt;Timeout::Error: execution expired&amp;gt; within timeout&lt;/code&gt;.  If &lt;code&gt;Timeout::Error&lt;/code&gt; extended from &lt;code&gt;StandardError&lt;/code&gt;, &lt;strong&gt;it would be caught by any generic rescue expression&lt;/strong&gt; that would handle exceptions occurring at the point where the timeout error happened to be raised.  This rescue clause may not even be in your application code&#8212;it could be in a library or the Rails framework, which could result in very hard to track down bugs.   We can verify this is the case by specifying what type of error &lt;code&gt;timeout&lt;/code&gt; should raise.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  require 'timeout'

  begin
    Timeout.timeout(1, StandardError) do
      begin
        sleep 2
      rescue =&amp;gt; e
        puts &quot;Caught #{e.inspect} within timeout&quot; 
      end
    end
  rescue =&amp;gt; e
    puts &quot;Caught #{e.inspect} at top level&quot; 
  end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The only differences here are that &lt;code&gt;Timeout.timeout&lt;/code&gt; is passed the type of exception to raise, &lt;code&gt;StandardError&lt;/code&gt;, and that the rescue clauses have be changed to omit the exception type.  The output is essentially the same as before, &lt;code&gt;Caught #&amp;lt;StandardError: execution expired&amp;gt; within timeout&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Incidentally, &lt;code&gt;SystemTimer&lt;/code&gt; terminates the offending thread by raising an exception as well.  If there is a rescue clause that handles &lt;code&gt;Timeout::Error&lt;/code&gt; within the block given for either &lt;code&gt;Timeout&lt;/code&gt; or &lt;code&gt;SystemTimer&lt;/code&gt;, it could prevent the error from reaching handling code outside the timeout call.  Fortunately the likelihood of encountering this scenario is minimized because &lt;code&gt;Timeout::Error&lt;/code&gt; doesn&#8217;t inherit from &lt;code&gt;StandardError&lt;/code&gt;.  I haven&#8217;t hit a bug caused by this in practice yet.  I&#8217;m sure if I had before I had thought through the situation it would have taken me a lot longer to figure out what was going on.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-06-27:37</id>
    <published>2008-06-27T03:25:00Z</published>
    <updated>2008-06-27T05:27:58Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/6/27/30-days-of-tech-day-26-fixnum-object_id" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 26 - Fixnum#object_id</title>
<summary type="html">&lt;p&gt;In Ruby every object has an object_id that uniquely identifies that object in that particular instance of the Ruby process.  Ruby also treats integers as objects&#8212;they are instances of &lt;code&gt;Fixnum&lt;/code&gt;.  For efficiency reasons, though, Ruby implements integers internally as simple values.  That is, it doesn&#8217;t allocate heap memory for every integer you use, it just passes the value around instead.  So how does Ruby generate object ids for all these integers?&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;In Ruby every object has an object_id that uniquely identifies that object in that particular instance of the Ruby process.  Ruby also treats integers as objects&#8212;they are instances of &lt;code&gt;Fixnum&lt;/code&gt;.  For efficiency reasons, though, Ruby implements integers internally as simple values.  That is, it doesn&#8217;t allocate heap memory for every integer you use, it just passes the value around instead.  So how does Ruby generate object ids for all these integers?&lt;/p&gt;
&lt;p&gt;You may have never noticed, but all the object ids that you usually end up seeing are even.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  irb(main):011:0&amp;gt; Object.object_id
  =&amp;gt; 110230
  irb(main):012:0&amp;gt; Object.new.object_id
  =&amp;gt; 191490
  irb(main):013:0&amp;gt; nil.object_id
  =&amp;gt; 4
  irb(main):014:0&amp;gt; &quot;a_string&quot;.object_id
  =&amp;gt; 177360
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This is because Ruby is reserving all the odd numbers to be object_ids for &lt;code&gt;Fixnums&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  irb(main):015:0&amp;gt; 1.object_id
  =&amp;gt; 3
  irb(main):016:0&amp;gt; 2.object_id
  =&amp;gt; 5
  irb(main):017:0&amp;gt; 3.object_id
  =&amp;gt; 7
  irb(main):018:0&amp;gt; 4.object_id
  =&amp;gt; 9
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Not only are they reserved, they&#8217;re calculated using a simple formula!  This allows Ruby to easily get back to the value from the id if you should ever ask for it.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  irb(main):022:0&amp;gt; ObjectSpace._id2ref(9)  
  =&amp;gt; 4
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;And now if you ever see an odd (as in n % 2 = 1, not strange) object_id in some Ruby output somewhere, you&#8217;ll know immediately what the value of that object is!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-06-26:36</id>
    <published>2008-06-26T01:27:00Z</published>
    <updated>2008-06-26T02:00:04Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/6/26/30-days-of-tech-day-25-gem-dependency-version" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 25 - Gem dependency version</title>
<summary type="html">&lt;p&gt;RubyGems and I had a fight.  It was about what version of &lt;code&gt;net-ssh&lt;/code&gt; we wanted to have installed on my machine.  RubyGems wanted the latest (2.0.2) and I wanted a 1.x version (1.1.4).  Apparently &lt;code&gt;net-ssh&lt;/code&gt; 2 has an issue with sshing into Fedora Core 4, which I needed it to do.  Specifically I needed to use capistrano &lt;code&gt;1.4.1&lt;/code&gt; to deploy an app to Fedora Core 4&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;RubyGems and I had a fight.  It was about what version of &lt;code&gt;net-ssh&lt;/code&gt; we wanted to have installed on my machine.  RubyGems wanted the latest (2.0.2) and I wanted a 1.x version (1.1.4).  Apparently &lt;code&gt;net-ssh&lt;/code&gt; 2 has an issue with sshing into Fedora Core 4, which I needed it to do.  Specifically I needed to use capistrano &lt;code&gt;1.4.1&lt;/code&gt; to deploy an app to Fedora Core 4&lt;/p&gt;
&lt;p&gt;This is something I&#8217;ve been doing regularly for some time, but ever since the &lt;a href='http://davidvollbracht.com/2008/6/10/30-days-of-tech-day-9-macbrick'&gt;MacBrick&lt;/a&gt; incident I haven&#8217;t needed these specific gems installed, so I was starting from scratch.  Once I figured out what version of the gems I wanted, here was my first attempt:
(I included &lt;code&gt;--no-rdoc&lt;/code&gt; and &lt;code&gt;--no-ri&lt;/code&gt; just to save time writing this post).&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ sudo gem install net-ssh --version=1.1.4 --no-rdoc --no-ri
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Successfully installed net-ssh-1.1.4
    1 gem installed
  $ sudo gem install capistrano --version=1.4.1 --no-rdoc --no-ri
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Successfully installed net-ssh-2.0.2
    Successfully installed net-sftp-2.0.1
    Successfully installed capistrano-1.4.1
    3 gems installed
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Clearly this didn&#8217;t work so well.  Installing capistrano installed the latest version of net-ssh.  But why? Capistrano&#8217;s gemspec for 1.4.1 says this&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  ...
  s.add_dependency(%q&amp;lt;net-ssh&amp;gt;, [&quot;&amp;gt;= 1.0.10&quot;])
  s.add_dependency(%q&amp;lt;net-sftp&amp;gt;, [&quot;&amp;gt;= 1.1.0&quot;])
  ...
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;But &lt;code&gt;net-sftp&lt;/code&gt; 2.0.1&#8217;s gemspec says it needs &lt;code&gt;net-ssh&lt;/code&gt; 1.99.1.  So we need to install specific version of both.  So, attempt number two.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ sudo gem install net-ssh --version=1.1.4 --no-rdoc --no-ri
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Successfully installed net-ssh-1.1.4
    1 gem installed
  $ sudo gem install net-sftp --version=1.1.0 --no-rdoc --no-ri
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Successfully installed net-ssh-2.0.2
    Successfully installed net-sftp-1.1.0
    2 gems installed
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Rats.  Even though &lt;code&gt;net-sftp&lt;/code&gt; 1.1.0 only specifies that it depends on &lt;code&gt;net-ssh&lt;/code&gt; &amp;gt;= 1.0.0, RubyGems went ahead and installed the latest version anyway.  It assumed that even though the dependency was satisfied, I want the latest version of &lt;code&gt;net-ssh&lt;/code&gt;&#8212; exactly what I&#8217;m trying to avoid.&lt;/p&gt;


	&lt;p&gt;Armed with this information, I made one last ditch attempt.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ sudo gem install capistrano --version=1.4.1 --no-rdoc --no-ri
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Successfully installed net-ssh-2.0.2
    Successfully installed net-sftp-2.0.1
    Successfully installed capistrano-1.4.1
    3 gems installed
  $ sudo gem install net-ssh --version=1.1.4 --no-rdoc --no-ri
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Successfully installed net-ssh-1.1.4
    1 gem installed
  $ sudo gem install net-sftp --version=1.1.0 --no-rdoc --no-ri
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Successfully installed net-sftp-1.1.0
    1 gem installed
  $ sudo gem uninstall net-sftp net-ssh

    Select gem to uninstall:
     1. net-sftp-1.1.0
     2. net-sftp-2.0.1
     3. All versions
    &amp;gt; 2
    Successfully uninstalled net-sftp-2.0.1

    Select gem to uninstall:
     1. net-ssh-1.1.4
     2. net-ssh-2.0.2
     3. All versions
    &amp;gt; 2
    Successfully uninstalled net-ssh-2.0.2
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Finally, by letting RubyGems install the latest version of &lt;code&gt;net-ssh&lt;/code&gt; and &lt;code&gt;net-sftp&lt;/code&gt;, then installing the versions I wanted, and the uninstalling the offending versions, I was able to get into a working state to deploy.  Hopefully I&#8217;ll never run into this problem again.  If you should be so unlucky, however, I hope this knowledge will help you.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-06-25:35</id>
    <published>2008-06-25T03:18:00Z</published>
    <updated>2008-06-25T04:00:04Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/6/25/30-days-of-tech-day-24-drubyall" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 24 - drubyall</title>
<summary type="html">&lt;p&gt;When we first started trying to use distributed DeepTest, we quickly ran into an issue with DRb servers not binding to the correct addresses.  DRb does provide a way to bind to &lt;code&gt;0.0.0.0&lt;/code&gt; to listen to all addresses, but there&#8217;s a downside to the way it does it.  The url you provide to DRb to accomplish this must omit the hostname (e.g. &lt;code&gt;druby://:0/&lt;/code&gt;).  DRb then determines what hostname to report to clients by calling &lt;code&gt;getaddrinfo&lt;/code&gt; with the empty hostname.  Unfortunately for us &lt;code&gt;getaddrinfo&lt;/code&gt; didn&#8217;t provide a hostname that could be used by our developer machines to connect to the DeepTest servers.  For a brief moment we thought we were stuck.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;When we first started trying to use distributed DeepTest, we quickly ran into an issue with DRb servers not binding to the correct addresses.  DRb does provide a way to bind to &lt;code&gt;0.0.0.0&lt;/code&gt; to listen to all addresses, but there&#8217;s a downside to the way it does it.  The url you provide to DRb to accomplish this must omit the hostname (e.g. &lt;code&gt;druby://:0/&lt;/code&gt;).  DRb then determines what hostname to report to clients by calling &lt;code&gt;getaddrinfo&lt;/code&gt; with the empty hostname.  Unfortunately for us &lt;code&gt;getaddrinfo&lt;/code&gt; didn&#8217;t provide a hostname that could be used by our developer machines to connect to the DeepTest servers.  For a brief moment we thought we were stuck.&lt;/p&gt;
&lt;p&gt;When we looked into how DRb handles protocols, however, we realized we could simply add a protocol that would accept a hostname as part of the url and use it to report the DRb url to clients when needed.  For listening on the network, however, the protocol always binds to &lt;code&gt;0.0.0.0&lt;/code&gt;.  By registering this protocol (which we called drubyall since it listens on all interfaces) you can use it in any DRb url, which is pretty handy.  Although it wasn&#8217;t nice that we ran into the problem, it was very nice that DRb gave us an easy out to extending its communications layer.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  module DeepTest
    class DRbBindAllTCPSocket &amp;lt; DRb::DRbTCPSocket
      def self.parse_uri(uri)
        if uri =~ /^drubyall:\/\/(.*?):(\d+)(\?(.*))?$/
          host = $1
          port = $2.to_i
          option = $4
          [host, port, option]
        else
          raise(DRb::DRbBadScheme, uri) unless uri =~ /^drubyall:/
          raise(DRb::DRbBadURI, 'can\'t parse uri:' + uri)
        end
      end

      # Open a server listening for connections at +uri+ using 
      # configuration +config+.
      def self.open_server(uri, config)
        uri = 'drubyall://:0' unless uri
        host, port, opt = parse_uri(uri)

        if host.size == 0
          host = getservername
        end

        DeepTest.logger.debug(&quot;Listening on port #{port}, all addresses.&quot;)
        soc = TCPServer.open('0.0.0.0', port)          
        port = soc.addr[1] if port == 0
        uri = &quot;druby://#{host}:#{port}&quot; 
        self.new(uri, soc, config)
      end
    end
  end

  DRb::DRbProtocol.add_protocol DeepTest::DRbBindAllTCPSocket
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://davidvollbracht.com/">
    <author>
      <name>dvollbracht</name>
    </author>
    <id>tag:davidvollbracht.com,2008-06-24:34</id>
    <published>2008-06-24T02:11:00Z</published>
    <updated>2008-06-24T03:00:04Z</updated>
    <category term="software"/>
    <link href="http://davidvollbracht.com/2008/6/24/30-days-of-tech-day-23-dearsoul" rel="alternate" type="text/html"/>
    <title>30 Days of Tech: Day 23 - DearSoul</title>
<content type="html">
            &lt;pre&gt;
&lt;code&gt;
  class DearSoul
    def self.new(first, last)
      ObjectSpace.define_finalizer(allocate, proc {|id|
        puts &quot;Goodbye, #{first}.  We'll miss you.&quot; 
      })
    end
  end

  DearSoul.new(&quot;George&quot;, &quot;Carlin&quot;)
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
</feed>
