<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Advogato blog for fxn</title>
    <link>http://www.advogato.org/person/fxn/</link>
    <description>Advogato blog for fxn</description>
    <language>en-us</language>
    <generator>mod_virgule</generator>
    <pubDate>Sat, 25 May 2013 06:39:31 GMT</pubDate>
    <item>
      <pubDate>Tue, 15 Nov 2011 22:07:09 GMT</pubDate>
      <title>15 Nov 2011</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=536</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=536</guid>
      <description>&lt;p&gt;&lt;b&gt;A Virtual Machine-based Development Environment&lt;/b&gt;&lt;/p&gt;&lt;p&gt;As many independent software developers, I normally have several projects going on: clients, side-projects, open source projects... Over the last years I have refined a development environment optimized for that use case that has worked really well for me. Let me share it in this post.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Isolated Environments&lt;/b&gt;&lt;/p&gt;&lt;p&gt;In a normal day I may work for a client that has a Rails 2.3.5 application with Solr, memcached, some custom vhosts, PostgreSQL 8.4, and a CAS running on Tomcat. Later at night I may work on Ruby on Rails and be able to, e.g., run the Active Record test suite with Ruby 1.9.3 and PostgreSQL 9.1.&lt;/p&gt;&lt;p&gt;If you multiply that by some number of projects, it becomes clear that a single machine can't possibly handle such a variety of environments in any predictable way. Well, you can predict chaos perhaps :).&lt;/p&gt;&lt;p&gt;And not only for today. If client C needs something three months after our last collaboration, I want to be able to launch his exact environment right away. No matter whether in the meantime I installed/uninstalled a gazillion things, upgraded the operating system, or got a new machine. I want to be able to launch the environment of client C anytime.&lt;/p&gt;&lt;p&gt;In summary, you need a robust setup that provides &lt;i&gt;isolation&lt;/i&gt; to every project you work on. Virtual machines are a solution to that problem that allow you to have one single development (real) computer.&lt;/p&gt;&lt;p&gt;My development environment is totally based on virtual machines.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Software Choices&lt;/b&gt;&lt;/p&gt;&lt;p&gt;My laptop is a 13'' MacBook Pro from mid-2009, 4 GB of RAM, running Lion nowadays.&lt;/p&gt;&lt;p&gt;I have &lt;a href="http://www.vmware.com/products/fusion/overview.html" &gt;VMware Fusion&lt;/a&gt; and all the virtual machines run Linux. Since as a user I love Mac OS X I do most of my work in the host (I'll tell you how in a minute). In the virtual machine I basically need just a console, a light desktop is hence enough. My distro of choice nowadays is &lt;a href="http://lubuntu.net/" &gt;Lubuntu&lt;/a&gt; running the &lt;tt&gt;open-vm-tools&lt;/tt&gt; Ubuntu package. That package provides desktop resize on window resize, copy &amp;amp; paste between the guest and the host, etc.&lt;/p&gt;&lt;p&gt;As I said, I do &lt;i&gt;not&lt;/i&gt; work inside the virtual machine. I launch the virtual machine and there I have the complete runtime I need. Web servers and test suites run in the virtual machine, but editing, browsing, etc. happens in the Mac. To accomplish that I have a couple of tricks.&lt;/p&gt;&lt;p&gt;First, sharing the file system. VMware allows you to mount your home in the guest via what they call &lt;i&gt;Shared Folders&lt;/i&gt;. But the virtual machines should be round and complete, so the code of my clients should be there, not in the host. In addition to that, Shared Folders do not work well with git, I think because of the hard linking going on. I rather go the other way around: I mount the guest file system in the host via &lt;a href="http://en.wikipedia.org/wiki/SSHFS" &gt;SSHFS&lt;/a&gt;. If you use MacPorts just run &lt;tt&gt;sudo port install sshfs&lt;/tt&gt; and you're golden. Looks like Homebrew also has a &lt;tt&gt;sshfs&lt;/tt&gt; package. It surely works, but I don't use Homebrew.&lt;/p&gt;&lt;p&gt;Now, SSHFS mounts the file system through (local) SSH, not as fast as the hard drive. But I only need the files for editing and I don't really care if saving a file takes 5 ms. So no big deal. The only detail you probably want to tweak is disabling automatic project tree sync on focus in your editor/IDE if it has the concept of a project tree and tree sync on focus.&lt;/p&gt;&lt;p&gt;Second, I have a personal rule: &lt;i&gt;One project at a time&lt;/i&gt;. If I am working for client C, I am totally focused on C's project. That's all I want to have up. Given that rule, I can implement a couple of convenient simplifications: All virtual machines have the same IP, and all virtual machines have the same mount point. Let me explain that in a dedicated section.&lt;/p&gt;&lt;p&gt;&lt;b&gt;One Single IP, One Single Mount Point&lt;/b&gt;&lt;/p&gt;&lt;p&gt;VMware has several network modes. My virtual machines are configured to run under the default NAT mode, in which VMware does DHCP for the guest. That by itself is a little bit cumbersome because you get different IPs in different sessions, and sometimes you even get the IP changed while the machine is running. I've found that fixed addresses work better for my needs, up to a point where all the virtual machines have the same one.&lt;/p&gt;&lt;p&gt;The configuration for DHCP as of this writing lives in the file &lt;i&gt;/Library/Preferences/VMware Fusion/vmnet8/dhcpd.conf&lt;/i&gt;, and to assign a fixed IP to a virtual machine you just need to know its MAC address. The MAC address can be manually set in the virtual machine settings, but I just grab whatever is printed by &lt;tt&gt;ifconfig eth0&lt;/tt&gt; after a standard installation.&lt;/p&gt;&lt;p&gt;Then, open the config file mentioned earlier and add towards the bottom something like this:&lt;pre&gt;&lt;br/&gt;
    host rails {&lt;br/&gt;
        hardware ethernet 00:0c:29:0a:98:b8;&lt;br/&gt;
        fixed-address 172.16.132.127;&lt;br/&gt;
    }&lt;/pre&gt;&lt;br/&gt;
That's the DHCP configuration for the virtual machine where I have the development environment for Ruby on Rails. The host name &amp;quot;rails&amp;quot; is an arbitrary string. It has MAC address &lt;tt&gt;00:0c:29:0a:98:b8&lt;/tt&gt; and a fixed IP of &lt;tt&gt;172.16.132.127&lt;/tt&gt;. I have a host configuration like that one per virtual machine.&lt;/p&gt;&lt;p&gt;To choose the IP have a look at the subnet block generated by VMware towards the middle of the file:&lt;pre&gt;&lt;br/&gt;
    subnet 172.16.132.0 netmask 255.255.255.0 {&lt;br/&gt;
        range 172.16.132.128 172.16.132.254;&lt;br/&gt;
        ...&lt;br/&gt;
    }&lt;/pre&gt;&lt;br/&gt;
According to the first line you have to choose an IP within &lt;tt&gt;172.16.132.*&lt;/tt&gt; that it is &lt;i&gt;outside&lt;/i&gt; the specified range. My choice is &lt;tt&gt;172.16.132.127&lt;/tt&gt;.&lt;/p&gt;&lt;p&gt;Since we have the same IP for any virtual machine, we can create an entry in &lt;i&gt;/etc/hosts&lt;/i&gt; that gives us a single hostname to rule them all:&lt;br/&gt;
&lt;pre&gt;&lt;br/&gt;
    172.16.132.127 vm&lt;br/&gt;
&lt;/pre&gt;&lt;br/&gt;
Mounting the file system is easily scriptable:&lt;br/&gt;
&lt;pre&gt;&lt;br/&gt;
    sshfs -o StrictHostKeyChecking=no -o reconnect -o workaround=rename fxn@vm:. $HOME/vm&lt;br/&gt;
&lt;/pre&gt;&lt;br/&gt;
Unmounting is also easily scriptable:&lt;br/&gt;
&lt;pre&gt;&lt;br/&gt;
    diskutil umount $HOME/vm 2&gt;/dev/null&lt;br/&gt;
&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Spurious Files&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Some programs in the host may create metadata files for the Mac like &lt;i&gt;.DS_Store&lt;/i&gt; and friends. I don't like such files in the virtual machines. For those few programs that do that there's normally a configuration option or somesuch to disable it. There's a shell one-liner for TextMate for example, but I no longer remember it because it's been a while since I used TextMate, but you can Google for it. AFAICT, Emacs, Vim, Sublime Text 2, and RubyMine leave &lt;i&gt;no&lt;/i&gt; spurious files out of the box.&lt;/p&gt;&lt;p&gt;As a last resort, if I get any of these spurious files for whatever reason I just run &lt;a href="https://github.com/fxn/unmac" &gt;&lt;i&gt;unmac&lt;/i&gt;&lt;/a&gt;. That's a little utility of mine implemented as a Ruby gem that cleans a given directory. To install it &lt;tt&gt;gem install unmac&lt;/tt&gt;, possibly with admin privs. In my experience I rarely need to run &lt;tt&gt;unmac&lt;/tt&gt; though.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Backups&lt;/b&gt;&lt;p&gt;&lt;p&gt;You do not want Time Machine to do incremental backups of your virtual machines because they are big files on disk that change continually. For backups I use &lt;a href="http://www.bombich.com/" &gt;Carbon Copy Cloner&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;That's another big win. Backups are trivial, no matter whether your hard disk breaks, or you get a new computer, you are ready to work in no time and with the guarantee that all those complicated environments are consistent and safe. The peace of mind that gives is invaluable.&lt;/p&gt;</description>
    </item>
    <item>
      <pubDate>Mon, 23 May 2011 22:05:44 GMT</pubDate>
      <title>23 May 2011</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=535</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=535</guid>
      <description>&lt;p&gt;&lt;b&gt;GeoPlanet data with ancestor chain cache imported in 10 minutes&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://gist.github.com/987613" &gt;GeoPlanet data with ancestor chain cache imported in 10 minutes&lt;/b&gt;&lt;/p&gt;&lt;p class="syndicated"&gt;&lt;a href="https://gist.github.com/987613" &gt;Syndicated 2011-05-24 00:04:00 by hand&lt;/a&gt;&lt;/p&gt; </description>
    </item>
    <item>
      <pubDate>Sun, 3 Oct 2010 01:10:35 GMT</pubDate>
      <title>3 Oct 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=534</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=534</guid>
      <description>&lt;p&gt;&lt;b&gt;Ruby, C, and Java are pass-by-value, Perl is pass-by-reference&lt;/b&gt;&#xD;
&lt;p&gt;Call semantincs in languages that manage references often confuse people. It is a recurring thread in Java and Ruby. The reason is simple: "pass-by-reference" has the word "reference" in it and thus people assume it has something to do with the language's "references". Not really.&#xD;
&lt;p&gt;In Ruby and Java "reference" is a term that is close to the concept of "pointer" in C. You have a handler that somehow points to something, rather than being that very something. The language may hide the indirection for you. Usage is straight in Ruby or Java. Not so in C or Perl, where to go &#xD;
from a pointer/reference to their objects you need an arrow.&#xD;
&lt;p&gt;When we talk about "pass-by-value", though, the fact that the language has references is irrelevant. We are really talking about where's the value associated with the parameters' name. Particularly we mean that the value is stored in an area unrelated to the storage in the callee. Let's see &#xD;
this.&#xD;
&lt;p&gt;Say we have an assignment&#xD;
&lt;pre&gt;&#xD;
    a = 1&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;This assignment means that somewhere there's an association between the name "a" and the value "1", which is itself stored somewhere:&#xD;
&lt;pre&gt;&#xD;
    +-----+       +-----+&#xD;
    |  a  | ----&amp;gt; |  1  |&#xD;
    +-----+       +-----+&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;If you assign&#xD;
&lt;pre&gt;&#xD;
    b = 1&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;conceptually we get two associations to two different value storages:&#xD;
&lt;pre&gt;&#xD;
    +-----+       +-----+&#xD;
    |  a  | ----&amp;gt; |  1  |&#xD;
    +-----+       +-----+&#xD;
 &#xD;
    +-----+       +-----+&#xD;
    |  b  | ----&amp;gt; |  1  |&#xD;
    +-----+       +-----+&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In particular, if in the next line we change &lt;tt&gt;b&lt;/tt&gt;:&#xD;
&lt;pre&gt;&#xD;
    b = 2&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;we all know the situation becomes&#xD;
&lt;pre&gt;&#xD;
    +-----+       +-----+&#xD;
    |  a  | ----&amp;gt; |  1  |&#xD;
    +-----+       +-----+&#xD;
 &#xD;
    +-----+       +-----+&#xD;
    |  b  | ----&amp;gt; |  2  |&#xD;
    +-----+       +-----+&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In particular &lt;tt&gt;a&lt;/tt&gt; still holds 1.&#xD;
&lt;p&gt;In languages like Perl you can have this other diagram:&#xD;
&lt;pre&gt;&#xD;
    +-----+&#xD;
    |  a  | --+&#xD;
    +-----+   |    +-----+&#xD;
              +--&amp;gt; |  1  |&#xD;
    +-----+   |    +-----+&#xD;
    |  b  | --+&#xD;
    +-----+&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In Perl jargon you say that &lt;tt&gt;a&lt;/tt&gt; and &lt;tt&gt;b&lt;/tt&gt; are &lt;i&gt;aliases&lt;/i&gt;. In that situation, any assignment to &lt;tt&gt;a&lt;/tt&gt; is reflected in &lt;tt&gt;b&lt;/tt&gt;, and any assignment to &lt;tt&gt;b&lt;/tt&gt; is reflected in &lt;tt&gt;a&lt;/tt&gt;. Those names are associated with the same storage area.&#xD;
&lt;p&gt;The terms "pass-by-value" and "pass-by-reference" are about names linked to storage. And with those pictures you can understand what they mean. I am gonna obviate scope to simplify this and use different variable names on purpose, so this is not exact, but the essence is there.&#xD;
&lt;p&gt;Say you have&#xD;
&lt;pre&gt;&#xD;
  def foo(b)&#xD;
    ...&#xD;
  end&#xD;
  &#xD;
  a = 1&#xD;
  foo(a)&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In a pass-by-value language the situation is:&#xD;
&lt;pre&gt;&#xD;
    +-----+       +-----+&#xD;
    |  a  | ----&amp;gt; |  1  |&#xD;
    +-----+       +-----+&#xD;
 &#xD;
    +-----+       +-----+&#xD;
    |  b  | ----&amp;gt; |  1  |&#xD;
    +-----+       +-----+&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;The interpreter or whoever runs your language performs a &lt;i&gt;copy&lt;/i&gt; behind the scenes of the storage area associated with "a", and associates the new one with "b". That's why if you reassing to &lt;tt&gt;b&lt;/tt&gt; inside &lt;tt&gt;foo&lt;/tt&gt; &lt;tt&gt;a&lt;/tt&gt; is unaffected.&#xD;
&lt;p&gt;On the other hand, in a pass-by-reference language the situation is:&#xD;
&lt;pre&gt;&#xD;
    +-----+&#xD;
    |  a  | --+&#xD;
    +-----+   |    +-----+&#xD;
              +--&amp;gt; |  1  |&#xD;
    +-----+   |    +-----+&#xD;
    |  b  | --+&#xD;
    +-----+&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;That's why you can implement &lt;tt&gt;swap&lt;/tt&gt; in such languages.&#xD;
&lt;p&gt;&lt;b&gt;But I can change the state of a mutable object in Ruby/Java because I pass a reference!&lt;/b&gt;&#xD;
&lt;p&gt;That is true, and it has no bearing with this. Since Ruby is pass-by-value, you can be certain that when the method returns &lt;i&gt;your variable will refer to the same object&lt;/i&gt;. &lt;tt&gt;object_id&lt;/tt&gt; is guaranteed to be the same after a method invocation (modulo black magic). Same for &#xD;
Java.&#xD;
&lt;p&gt;&lt;b&gt;But I can change the integer a variable holds by passing a pointer in C!&lt;/b&gt;&#xD;
&lt;p&gt;That is true, but you are not passing the integer, you are passing a pointer to the integer. Since C is pass-by-value, if you had a variable holding the pointer before the call, you can be totally certain the variable will hold the same exact pointer after the call.&#xD;
&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;&#xD;
&lt;p&gt;The terms pass-by-value and pass-by-reference are about links from names to storage areas, they have nothing to do with the references or pointers of your language.&#xD;
&lt;p&gt;That's a bit simplified, in Perl for example the aliases happen within &lt;tt&gt;@_&lt;/tt&gt;, but that's the key idea.</description>
    </item>
    <item>
      <pubDate>Sun, 8 Aug 2010 19:41:12 GMT</pubDate>
      <title>8 Aug 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=533</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=533</guid>
      <description>&lt;p style="font-size:large;font-weight:bold"&gt;When Classes Leak Into Ruby &#xD;
Contracts&#xD;
&lt;p&gt;Sometimes Ruby APIs document non-Rubyesque expectations that &#xD;
artificially separate "classes" and "objects", a la Java. I'd like to give you a few &#xD;
examples to depict what I mean, and explain why that's artificial later.&#xD;
&lt;p&gt;First example, &lt;a href "http://rack.rubyforge.org/doc/SPEC.html"&gt;the &#xD;
Rack specification&lt;/a&gt; says that&#xD;
&lt;blockquote&gt;&#xD;
&lt;i&gt;A Rack application is an Ruby object (not a class) that responds to &#xD;
&lt;tt&gt;call&lt;/tt&gt;.&lt;/i&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;p&gt;That's not very idiomatic, why classes are banned? A Ruby programmer &#xD;
would expect this shorter contract:&#xD;
&lt;blockquote&gt;&#xD;
&lt;i&gt;A Rack application is an Ruby object that responds to &lt;tt&gt;call&lt;/tt&gt;.&lt;/i&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;p&gt;That's it, the nature of the object is irrelevant to Rack, the only thing that &#xD;
matters is that the object responds to &lt;tt&gt;call&lt;/tt&gt; (with such and such &#xD;
signature). Indeed a class that responds to &lt;tt&gt;call&lt;/tt&gt; is a perfectly valid &#xD;
Rack application. The implementation is Rubyesque, but the wording in the &#xD;
docs is not.&#xD;
&lt;p&gt;Another example taken from &lt;a href="http://rails-&#xD;
nutshell.labs.oreilly.com/ch04.html" &gt;the chapter on routing of O'Reilly's &#xD;
&lt;i&gt;Rails 3 in a Nutshell&lt;/i&gt;&lt;/a&gt;:&#xD;
&lt;blockquote&gt;&#xD;
&lt;i&gt;Constraints may either be specified as a hash, a class implementing a &#xD;
&lt;tt&gt;matches?&lt;/tt&gt; class method, or a class that responds to a call method, &#xD;
such as a Proc object.&lt;/i&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;p&gt;The suspicious bit in that contract is "a class implementing a &#xD;
&lt;tt&gt;matches?&lt;/tt&gt; class method". Indeed there's no requirement in the &#xD;
routing system that you pass a class. All it matters is that you pass &#xD;
&lt;i&gt;any&lt;/i&gt; object that responds to &lt;tt&gt;matches?&lt;/tt&gt;, see:&#xD;
&lt;pre&gt;&#xD;
    constraint.respond_to?(:matches?) &amp;amp;&amp;amp; !constraint.matches?(req)&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;That's idiomatic Ruby, where the interface is the only thing that matters, &#xD;
classes are irrelevant.&#xD;
&lt;p style="font-size:large;font-weight:bold"&gt;Classes Are Ordinary &#xD;
Objects&#xD;
&lt;p&gt;Technically in Ruby there are no "class methods" as opposed to "instance &#xD;
methods". Ruby only has instance methods. Let me summarize how this &#xD;
works.&#xD;
&lt;p&gt;If you define a &lt;tt&gt;Person&lt;/tt&gt; class having a &lt;tt&gt;name&lt;/tt&gt; instance &#xD;
method, instances of &lt;tt&gt;Person&lt;/tt&gt; respond to &lt;tt&gt;name&lt;/tt&gt;. No &#xD;
surprises here. But individual person instances can respond to more &#xD;
stuff:&#xD;
&lt;pre&gt;&#xD;
    def person.custom_method&#xD;
      ...&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In the example above, the object stored in the &lt;tt&gt;person&lt;/tt&gt; variable &#xD;
also responds to &lt;tt&gt;custom_method&lt;/tt&gt;. Such a method targeted to a &#xD;
particular instance is called a &lt;i&gt;singleton method&lt;/i&gt;. You can for example &#xD;
build simple mocks this way:&#xD;
&lt;pre&gt;&#xD;
    o = Object.new&#xD;
    def o.name&#xD;
      "John"&#xD;
    end&#xD;
    # Now pass o to code that expects anything responding to #name.&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;And you can also override methods defined in the class of the &#xD;
object.&#xD;
&lt;p&gt;In Ruby classes are objects. When you write &lt;tt&gt;Person&lt;/tt&gt;, that's an &#xD;
&lt;i&gt;ordinary constant&lt;/i&gt;. Totally ordinary. It is the same kind of ordinary &#xD;
constant as&#xD;
&lt;pre&gt;&#xD;
    X = 1&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;No difference. You can think of&#xD;
&lt;pre&gt;&#xD;
    class Person&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;as being equivalent to&#xD;
&lt;pre&gt;&#xD;
    Person = Class.new&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;The Ruby interpreter then processes the class definition body, but as far &#xD;
as the constant is concerned that's it. In fact, if you have an anonymous class &#xD;
and assign it to a constant later, then it gets its name automatically after the &#xD;
constant's name.&#xD;
&lt;p&gt;So, this is a key point, the &lt;tt&gt;Person&lt;/tt&gt; constant is &#xD;
&lt;i&gt;ordinary&lt;/i&gt;, it happens to evaluate to a class object, the same way &#xD;
&lt;tt&gt;X&lt;/tt&gt; above evaluates to an integer. And here is when Ruby deviates &#xD;
from other OO languages: classes are &lt;i&gt;ordinary objects&lt;/i&gt; also, objects &#xD;
of type &lt;tt&gt;Class&lt;/tt&gt;:&#xD;
&lt;pre&gt;&#xD;
    klass = Person&#xD;
    person = klass.new&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;That works because &lt;tt&gt;Person&lt;/tt&gt; just evaluates to a class object, &#xD;
and as with any other object you can store classes in variables and pass them &#xD;
around, and that object responds to the &lt;tt&gt;new&lt;/tt&gt; method, the same &#xD;
way &lt;tt&gt;person&lt;/tt&gt; responds to &lt;tt&gt;name&lt;/tt&gt;. Why? Because &#xD;
&lt;tt&gt;klass&lt;/tt&gt; is an object of the class &lt;tt&gt;Class&lt;/tt&gt;, which defines &#xD;
&lt;tt&gt;new&lt;/tt&gt; among its instance methods. Simple and elegant.&#xD;
&lt;p&gt;So at this point you need to forget a bit mental schemas coming from &#xD;
other languages and open your mind to accept the derivations of this &#xD;
particular OO model.&#xD;
&lt;p&gt;Since classes are ordinary objects, you can also define singleton methods &#xD;
on them, the same way we did with &lt;tt&gt;person&lt;/tt&gt; before. Do you &#xD;
recognize now this idiom?&#xD;
&lt;pre&gt;&#xD;
    class Person&#xD;
      def self.find_by_name(name)&#xD;
        ...&#xD;
      end&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In the body of a class &lt;tt&gt;self&lt;/tt&gt; is the class object in scope, and so &#xD;
that is just defining a singleton method on it.&#xD;
&lt;p&gt;All classes are instances of &lt;tt&gt;Class&lt;/tt&gt;, a "class method" is any &#xD;
method a class responds to, which may come from &lt;tt&gt;Class&lt;/tt&gt;, or be &#xD;
defined for particular classes, that is, singleton methods.&#xD;
&lt;p style="font-size:large;font-weight:bold"&gt;Class Methods, Fine&#xD;
&lt;p&gt;I am fine with the term "class method" in Ruby as long as we know what &#xD;
we are talking about. Technically Ruby has no such things, everything are &#xD;
instance methods, but you can take "class method" as short for "an instance &#xD;
method of the class object". Depending on their intended usage, class &#xD;
methods are also referred to as "macros", also a convenient term, think &#xD;
&lt;tt&gt;has_many&lt;/tt&gt; in Active Record.&#xD;
&lt;p&gt;But even if you can talk about "class methods" in that sense, you rarely &#xD;
need to tell classes from non-classes in API contracts based on &#xD;
interfaces.&#xD;
</description>
    </item>
    <item>
      <pubDate>Sat, 12 Jun 2010 23:14:32 GMT</pubDate>
      <title>12 Jun 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=532</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=532</guid>
      <description>&lt;p&gt;&lt;b&gt;Ruby Hero 2010&lt;/b&gt;&#xD;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/oreillyconf/4684097612/in/faves-25468319@N02/" &gt;&lt;img src="http://farm5.static.flickr.com/4041/4684097612_0e1ba393bc_t.jpg"&gt;&lt;/a&gt;&#xD;
&lt;a href="http://www.flickr.com/photos/oreillyconf/4684089494/in/faves-25468319@N02/" &gt;&lt;img src="http://farm5.static.flickr.com/4070/4684089494_41331dd0d0_t.jpg"&gt;&lt;/a&gt;&#xD;
&lt;a href="http://www.flickr.com/photos/25468319@N02/4694033587/" &gt;&lt;img src="http://farm5.static.flickr.com/4038/4694033587_ec1597139d_t.jpg"&gt;&lt;/a&gt;&#xD;
&lt;p&gt;I was awarded Ruby Hero 2010 in RailsConf this week. I am deeply &#xD;
grateful &#xD;
and honored by this recognition I received together with &lt;a href="http://twitter.com/josevalim" &gt;Jos&amp;eacute; Valim&lt;/a&gt; (devise, Rails &#xD;
core team), &lt;a href="http://twitter.com/qrush" &gt;Nick Quaranto&lt;/a&gt; (RubyGems.org), &lt;a href="http://twitter.com/tenderlove" &gt;Aaron Patterson&lt;/a&gt; &#xD;
(Nokogiri, SQLite driver, and a ton of other software), &lt;a href="http://twitter.com/wayneeseguin" &gt;Wayne Seguin&lt;/a&gt; (RVM &#xD;
awesomeness), and &lt;a href="http://twitter.com/seacreature" &gt;Gregory &#xD;
Brown&lt;/a&gt; (Ruport, Prawn, Ruby Best &#xD;
Practices...). </description>
    </item>
    <item>
      <pubDate>Thu, 6 May 2010 11:27:40 GMT</pubDate>
      <title>6 May 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=531</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=531</guid>
      <description>&lt;p&gt;&lt;i&gt;Progress [on The Last Supper] was steady but slow, as the artist &#xD;
worked on in his typical thoughtful and meditative way. He spent considerable &#xD;
time roaming the streets of Milan looking for suitable models for faces of the &#xD;
apostles. By 1497 the only part left to complete was the head of Judas. At that &#xD;
point, the prior of the convent became so impatient with Leonardo's slowness &#xD;
that he complained to the duke, who summoned the artist to hear his reasons &#xD;
for the delay. According to Vasari, Leonardo explained to the Moor that he was &#xD;
working on The Last Supper at least two hours a day, but that most of his work &#xD;
took place in his mind. He went on, slyly, to say that, if he did not find an &#xD;
appropriate model for Judas, he would give the villain the features of the &#xD;
petulant prior. Ludovico was so amused by Leonardo's reply that he instructed &#xD;
the prior to be patient and let Leonardo finish his work &#xD;
undisturbed.&lt;/i&gt;&#xD;
&lt;p&gt;&lt;a href="http://www.fritjofcapra.net/leonardo.html" &gt;The Science of &#xD;
Leonardo&lt;/a&gt;, Fritjof Capra, pages 95--96.</description>
    </item>
    <item>
      <pubDate>Sun, 11 Apr 2010 16:31:33 GMT</pubDate>
      <title>11 Apr 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=530</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=530</guid>
      <description>&lt;p&gt;&lt;b&gt;Rails Committer&lt;/b&gt;&#xD;
&lt;p&gt;I've been contributing to Ruby on Rails on a regular&#xD;
basis, had almost 100 code patches, and about&#xD;
500 doc patches, and was recently granted commit right.&#xD;
That's so great! It will allow me to work with more agility&#xD;
and have a little more freedom to do stuff.&#xD;
</description>
    </item>
    <item>
      <pubDate>Wed, 10 Mar 2010 15:04:29 GMT</pubDate>
      <title>10 Mar 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=529</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=529</guid>
      <description>&lt;p&gt;&lt;b&gt;An around filter to temporarily apply changes&lt;/b&gt;&#xD;
&lt;p&gt;A client of mine has an admin tool where people can edit stuff, but &#xD;
modifications need &#xD;
approval. They are represented in the application rather than applied right &#xD;
away as you normally do.&#xD;
&lt;p&gt;Changes are basically stored in the form of method names, ID of &#xD;
receiver, &#xD;
and serialized arguments, there are a handful of them. Applying changes is &#xD;
dynamic invocation, you get the idea.&#xD;
&lt;p&gt;My client wanted an approval interface where he could select a handful of &#xD;
changes to the same model, submit, and get a split view with the current &#xD;
website on the left, and the resulting website on the right.&#xD;
&lt;p&gt;The tricky part is that views may access the database, for example using &#xD;
named scopes (from a MVC viewpoint that's fine, the view is clean it happens &#xD;
that a &#xD;
named scope triggers a query). And the 3-line solution was to write an &#xD;
around filter that started &#xD;
a transaction, yielded to the action, and rolled the transaction back.&#xD;
</description>
    </item>
    <item>
      <pubDate>Sat, 30 Jan 2010 18:10:06 GMT</pubDate>
      <title>30 Jan 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=528</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=528</guid>
      <description>&lt;p&gt;&lt;b&gt;Tracking Class Descendants in Ruby (II)&lt;/b&gt;&#xD;
&lt;p&gt;&lt;a href="http://www.advogato.org/person/fxn/diary/527.html" &gt;My previous post&lt;/a&gt; explains a way to keep &#xD;
track of a class' &#xD;
descendants, and &#xD;
encapsulates the technique into a module.&#xD;
&lt;p&gt;There are two things you may want to do different: Since all descendants inherit the &lt;tt&gt;descendants&lt;/tt&gt; &#xD;
class method you may prefer &#xD;
them to be &#xD;
functional. On the other hand, the module defines the &lt;tt&gt;inherited&lt;/tt&gt; class method into the base class &#xD;
because it needs it to be a &#xD;
closure. That may &#xD;
work for some particular need, but it is not good for a generic solution. The &lt;tt&gt;inherited&lt;/tt&gt; hook is the &#xD;
business of your client's code.&#xD;
&lt;p&gt;Now we'll see a different approach that addresses both concerns. Using the same hook any class in the &#xD;
hierarchy may easily keep track of &#xD;
its direct &#xD;
subclasses, and compute its descendants:&#xD;
&lt;pre&gt;&#xD;
    class C&#xD;
      def self.inherited(subclass)&#xD;
        subclasses &amp;lt;&amp;lt; subclass&#xD;
      end&#xD;
 &#xD;
      def self.subclasses&#xD;
        @subclasses ||= []&#xD;
      end&#xD;
 &#xD;
      def self.descendants&#xD;
        subclasses + subclasses.map(&amp;amp;:descendants).flatten&#xD;
      end&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In the previous solution the &lt;tt&gt;inherited&lt;/tt&gt; hook needed to ensure &lt;tt&gt;descendants&lt;/tt&gt; was invoked &#xD;
on the root of the hierarchy. In &#xD;
this solution &#xD;
it doesn't care because we precisely take advantage of polymorphism. The way it is written a class pushes into its &#xD;
own &lt;tt&gt;@subclasses&lt;/tt&gt; instance &#xD;
variable, which is what we want.&#xD;
&lt;p&gt;The module that encapsulates that pattern is much simpler:&#xD;
&lt;pre&gt;&#xD;
    module DescendantsTracker&#xD;
      def inherited(subclass)&#xD;
        subclasses &amp;lt;&amp;lt; subclass&#xD;
        super&#xD;
      end&#xD;
 &#xD;
      def subclasses&#xD;
        @subclasses ||= []&#xD;
      end&#xD;
 &#xD;
      def descendants&#xD;
        subclasses + subclasses.map(&amp;amp;:descendants).flatten&#xD;
      end&#xD;
    end&#xD;
 &#xD;
    class C&#xD;
      extend DescendantsTracker&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;You know &lt;tt&gt;extend&lt;/tt&gt; is like doing an &lt;tt&gt;include&lt;/tt&gt; in the metaclass of &lt;tt&gt;C&lt;/tt&gt;. In particular &#xD;
we are not defining &#xD;
&lt;tt&gt;C.inherited&lt;/tt&gt;, &#xD;
we are defining a method with the same name in an ancestor of the metaclass. That way &lt;tt&gt;C&lt;/tt&gt; can still &#xD;
define its own &#xD;
&lt;tt&gt;inherited&lt;/tt&gt; class &#xD;
method. A call to &lt;tt&gt;super&lt;/tt&gt; within such a &lt;tt&gt;C.inherited&lt;/tt&gt; will go up the next ancestor of the &#xD;
metaclass, eventually reaching the &#xD;
&lt;tt&gt;inherited&lt;/tt&gt; from &lt;tt&gt;DescendantsTracker&lt;/tt&gt;.&#xD;
&#xD;
</description>
    </item>
    <item>
      <pubDate>Fri, 29 Jan 2010 01:34:26 GMT</pubDate>
      <title>29 Jan 2010</title>
      <link>http://www.advogato.org/person/fxn/diary.html?start=527</link>
      <guid>http://www.advogato.org/person/fxn/diary.html?start=527</guid>
      <description>&lt;p&gt;&lt;b&gt;Tracking Class Descendants in Ruby&lt;/b&gt;&#xD;
&lt;p&gt;I am going through all Active Support core extensions &#xD;
lately because I am writing the Active Support Core &#xD;
Extensions guide, due for Rails 3. There are some patches in &#xD;
master as a result of that walkthrough, and I am now &#xD;
focusing on keeping track of descendants in a class &#xD;
hierarchy.&#xD;
&lt;p&gt;A known technique uses &#xD;
&lt;tt&gt;ObjectSpace.each_object&lt;/tt&gt;. That is a method that &#xD;
receives a class or module as argument and yields all &#xD;
objects that have that class or module among their parents. &#xD;
Since classes are instances of the class &lt;tt&gt;Class&lt;/tt&gt;, you &#xD;
can select descendants of class &lt;tt&gt;C&lt;/tt&gt; this way:&#xD;
&lt;pre&gt;&#xD;
    descendants_of_C = []&#xD;
    ObjectSpace.each_object(Class) do |klass|&#xD;
      descendants_of_C &amp;lt;&amp;lt; klass if klass &amp;lt; C&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;That is a brute force approach, it works, but it is &#xD;
inefficient. JRuby even disables &lt;tt&gt;ObjectSpace&lt;/tt&gt; by &#xD;
default &lt;a href="http://kenai.com/projects/jruby/pages/Performance&#xD;
Tuning#Disabling_ObjectSpace" &gt;for performance &#xD;
reasons&lt;/a&gt;.&#xD;
&lt;p&gt;A better approach is to leverage the &#xD;
&lt;tt&gt;inherited&lt;/tt&gt; hook. Classes may optionally &#xD;
implement a class method &lt;tt&gt;inherited&lt;/tt&gt; that is called &#xD;
whenever they are subclassed. The subclass is passed as &#xD;
argument:&#xD;
&lt;pre&gt;&#xD;
    class User&#xD;
      def self.inherited(subclass)&#xD;
        puts 0&#xD;
      end&#xD;
    end&#xD;
 &#xD;
    class Admin &amp;lt; User&#xD;
      puts 1&#xD;
    end&#xD;
 &#xD;
    # output is&#xD;
    0&#xD;
    1&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;That's a perfect place to keep track of &#xD;
descendants:&#xD;
&lt;pre&gt;&#xD;
    class C&#xD;
      class &amp;lt;&amp;lt; self&#xD;
        def inherited(subclass)&#xD;
          C.descendants &amp;lt;&amp;lt; subclass&#xD;
          super&#xD;
        end&#xD;
 &#xD;
        def descendants&#xD;
          @descendants ||= []&#xD;
        end&#xD;
      end&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;In that code we have an array of descendants in &#xD;
&lt;tt&gt;@descendants&lt;/tt&gt;. That is an instance variable of the &#xD;
very class &lt;tt&gt;C&lt;/tt&gt;. Remember classes are ordinary &#xD;
objects in Ruby and so they may have instance variables. It &#xD;
is better to use an instance variable instead of a class &#xD;
variable because class variables are shared among the &#xD;
entire hierarchy of the class and we need an exclusive &#xD;
array.&#xD;
&lt;p&gt;Another fine point is that we force &#xD;
&lt;tt&gt;descendants&lt;/tt&gt; to be the one in the &lt;tt&gt;C&lt;/tt&gt; &#xD;
class. If we didn't and we had &lt;tt&gt;A &amp;lt; B &amp;lt; C&lt;/tt&gt;, the &#xD;
hook would be called when &lt;tt&gt;A&lt;/tt&gt; was defined, but by &#xD;
polymorphism it would be &lt;tt&gt;B.descendants&lt;/tt&gt; what &#xD;
would be called, thus setting &lt;tt&gt;B&lt;/tt&gt;'s instance variable &#xD;
&lt;tt&gt;@descendants&lt;/tt&gt;. That is not what we want.&#xD;
&lt;p&gt;The call to &lt;tt&gt;super&lt;/tt&gt; is just a best practice. In &#xD;
general a hook like this should pass the call up the &#xD;
hierarchy in case parents have their own hooks.&#xD;
&lt;p&gt;That pattern can be implemented in a module for reuse &#xD;
indeed:&#xD;
&lt;pre&gt;&#xD;
    module DescendantsTracker&#xD;
      def self.included(base)&#xD;
        (class &amp;lt;&amp;lt; base; self; end).class_eval do&#xD;
          define_method(:inherited) do |subclass|&#xD;
            base.descendants &amp;lt;&amp;lt; subclass&#xD;
            super&#xD;
          end&#xD;
        end&#xD;
        base.extend self&#xD;
      end&#xD;
 &#xD;
      def descendants&#xD;
        @descendants ||= []&#xD;
      end&#xD;
    end&#xD;
 &#xD;
    class C&#xD;
      include DescendantsTracker&#xD;
    end&#xD;
&lt;/pre&gt;&#xD;
&lt;p&gt;A class only needs to include &#xD;
&lt;tt&gt;DescendantsTracker&lt;/tt&gt; to track its &#xD;
descendants.&#xD;
&lt;p&gt;When the module is included in a class Ruby invokes its &#xD;
&lt;tt&gt;inherited&lt;/tt&gt; hook. The hook receives the class that &#xD;
is including the module, and we leverage that to inject the &#xD;
class methods we saw before. For &lt;tt&gt;inherited&lt;/tt&gt; we &#xD;
open the metaclass of &lt;tt&gt;base&lt;/tt&gt; and define the &#xD;
method in a way that has &lt;tt&gt;base&lt;/tt&gt; in scope, which is &#xD;
something we saw before we need. After that we add the &#xD;
&lt;tt&gt;descendants&lt;/tt&gt; class method with an ordinary &#xD;
&lt;tt&gt;extend&lt;/tt&gt; call.&#xD;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; There's a &lt;a href="http://www.advogato.org/person/fxn/diary/528.html" &gt;followup to this &#xD;
post&lt;/a&gt;.&#xD;
</description>
    </item>
  </channel>
</rss>
