<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>self.collect(&#38;:code)</title>
	<atom:link href="http://benanne.net/code/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://benanne.net/code</link>
	<description>Sander Dieleman&#039;s glorified private pastebin. Or blog. Whatever.</description>
	<lastBuildDate>Sun, 13 Sep 2009 21:05:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Custom tags for node links in Drupal 6</title>
		<link>http://benanne.net/code/?p=408</link>
		<comments>http://benanne.net/code/?p=408#comments</comments>
		<pubDate>Sun, 13 Sep 2009 17:12:49 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=408</guid>
		<description><![CDATA[In this post I&#8217;m going to try and explain how to add custom tags that link to node pages to a Drupal 6 input format. That probably sounds a bit far-fetched, so I&#8217;ll attempt to clarify by using my own project as an example.
last.fm has this nifty feature where you can write [artist]Meshuggah[/artist] and it&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;m going to try and explain how to add custom tags that link to node pages to a Drupal 6 input format. That probably sounds a bit far-fetched, so I&#8217;ll attempt to clarify by using my own project as an example.</p>
<p><a title="last.fm" href="http://last.fm" target="_blank">last.fm</a> has this nifty feature where you can write <code>[artist]Meshuggah[/artist]</code> and it&#8217;ll turn into a link to that artist&#8217;s last.fm page. A set of custom BBCode tags, basically. The project I&#8217;m working on, <a title="got-djent.com" href="http://got-djent.com/" target="_blank">got-djent.com</a>, is also a website about music, and will contain a band/album database maintained by the users in a wiki-like fashion. On the front page, it will be possible to post news articles. I thought it would be nice if band and album pages could be linked from these articles easily, by using custom tags.</p>
<h3>Installing the <code>customfilter</code> module</h3>
<p>What I set out to implement was having <code>&lt;band&gt;Meshuggah&lt;/band&gt;</code> turn into <code>&lt;a href="http://got-djent.com/node/2"&gt;Meshuggah&lt;/a&gt;</code>, e.g. a link to that band&#8217;s page. Something like this is best implemented as a &#8220;custom filter&#8221; in Drupal 6. To be able to do this, you have to install and enable the <code>customfilter</code> module first. You can <a href="http://drupal.org/project/customfilter">get it here</a>. Once you have uploaded it to the <code>modules/</code> directory of your Drupal install, you can enable it under <em>Administer &gt; Site Building &gt; Modules</em> (admin/build/modules).</p>
<p><span id="more-408"></span></p>
<div id="attachment_474" class="wp-caption alignright" style="width: 185px"><img src="http://benanne.net/code/wp-content/uploads/2009/09/druplicon.small.png" alt="Drupal is pretty nifty." title="Drupal" width="175" height="200" class="size-full wp-image-474" /><p class="wp-caption-text">Drupal is pretty nifty.</p></div>
<h3>Creating the filter</h3>
<p>Now we can create a custom filter. To do this, go to <em>Administer &gt; Site configuration &gt; Custom filters</em> (admin/settings/customfilter) and click <em>Add filter</em>. Fill out the fields (only the Name field is compulsory). I called my filter &#8220;Node links&#8221;. It is also a good idea to disable filter caching here (it is enabled by default), for the time being. This makes debugging a lot less frustrating, should any issues arise. Once you have verified that the filter works correctly, you can enable caching again.</p>
<p>To use the filter, you have to add it to a <em>input format</em>. I chose to be lazy and just add it to the builtin &#8220;Filtered HTML&#8221; input format, since that was already being used everywhere. To do this, Go to <em>Administer &gt; Site configuration &gt; Input formats</em> (admin/settings/filters) and click the <em>configure</em> link for &#8220;Filtered HTML&#8221;. Under the <em>Filters</em> heading, Tick the box next to your custom filter and click the <em>Save configuration</em> button. </p>
<p>To make sure the filter is applied at the right time, click <em>configure</em> again, and this time click the <em>Rearrange</em> link at the top. This will allow you to rearrange the order in which the filters are applied. For some reason, <strong>the default order rendered my filter useless</strong> so I had to change this. I dragged my custom filter all the way to the top. This is probably a bit overkill, but at least it works properly now.</p>
<h3>Adding replacement rules</h3>
<p>For the filter to actually do anything, you have to add replacement rules to it. In the Custom filters overview, click the filter&#8217;s name, then click <em>Add rule</em> at the top. Give the rule a name (my filter has two rules, called &#8220;bands&#8221; and &#8220;releases&#8221;). Now comes the interesting part: you have to specify a pattern and a replacement text. </p>
<p>The pattern is specified as a regular expression. More information about regular expressions can be found at <a href="http://www.regular-expressions.info/">regular-expressions.info</a>. If you don&#8217;t know what they are, you might want to familiarise yourself with the concept before you continue reading. The pattern I used for the &#8220;bands&#8221; rule was relatively simple:</p>
<pre>#&lt;band&gt;(.*?)&lt;/band&gt;#i</pre>
<p>This matches text between <code>&lt;band&gt;&lt;/band&gt;</code> tags. The <code>#</code>&#8217;s are the delimiters. The <code>i</code> modifier flag at the end makes the expression case insensitive. The parentheses form a capture group, which means that whatever is matched between them will be available when we construct the replacement text. Within the capture group, we use lazy matching (<code>*?</code> is the lazy version of <code>*</code>) to make sure that the filter works correctly when the tag is used multiple times. The <code>*</code> wildcard is greedy by default, which means that it attempts to consume as many characters as possible; <code>&lt;band&gt;foo&lt;/band&gt; &lt;band&gt;bar&lt;/band&gt;</code> would result in a single match, where <code>(.*)</code> matches <code>foo&lt;/band&gt; &lt;band&gt;bar</code>! Adding the question mark prevents this.</p>
<p>Next, we have to specify the replacement text. We have two options here: the simplest is just entering some text, in which <code>$1</code> or <code>${1}</code> is included as a placeholder, which will be replaced by whatever was matched inside the first capture group of the regular expression. Note that you can also use <code>$2</code>, <code>$3</code>, &#8230; when you have multiple capture groups. <code>$0</code> always contains the entire match. Unfortunately, this method isn&#8217;t suited for what we&#8217;re trying to do. Luckily, there is a more advanced option available as well: we can tick the <em>PHP Code</em> box, and then enter some custom PHP code to &#8220;compute&#8221; the replacement text instead.</p>
<h3>Computing the replacement text</h3>
<p>The <code>customfilter</code> module now expects us to write some PHP code which computes the replacement text and stores it in the <code>$result</code> variable. It can access the regular expression matches through the <code>$matches</code> array. <code>$matches[n]</code> corresponds to <code>$n</code>, so the value we are interested in here is <code>$matches[1]</code>. We have to find a node with the <code>band</code> node type, whose title corresponds to this value.</p>
<p>The simplest way to do this is to use an alternative (and badly documented) syntax for Drupal&#8217;s <code>node_load</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nd</span> <span style="color: #339933;">=</span> node_load<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'band'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The title matching is case insensitive, which is convenient. However, this only works properly if we can find an exact (case insensitive) match. Seeing as bands and albums can have pretty long names sometimes (like, say, <em>Fredrik Thordendal&#8217;s Special Defects</em>), it would be even more convenient if we could specify the name only partially. The solution is to use Drupal&#8217;s search functionality.</p>
<p>I&#8217;m not sure, but I think you have to <strong>enable the <code>search</code> optional core module</strong> for this to work. Instead of immediately calling <code>node_load</code>, we run a search first. For this, we can use the <code>do_search</code> function. Note that we need some trickery here to limit the search results to certain nodetypes (the 3rd and 4th arguments):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> do_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'node'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;INNER JOIN {node} n ON n.nid = i.sid&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;(n.type = 'band')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>do_search</code> only returns the 10 best results, which is fine because we only need a single one in this case. If you intend to use it for other purposes, this is something to take into account, though. The results are returned as an array. The <code>sid</code> field of a result corresponds to the node id of the matched node. This means that we can acquire the node object we need as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nd</span> <span style="color: #339933;">=</span> node_load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now all we need to do is use the node information to create a link. Drupal provides a convenient function for this called <code>l</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> l<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'node/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$nd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, we can enter <code>&lt;band&gt;thordendal&lt;/band&gt;</code>, and it will be replaced by a correct link: <code>&lt;a href="http://got-djent.com/node/36"&gt;Fredrik Thordendal's Special Defects&lt;/a&gt;</code>. Nice. There are a few edge cases that we haven&#8217;t taken into account yet, though: what if there are no matches, for example? The nice thing to do is then to just return the text between the tags, without linking anywhere:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> do_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'node'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;INNER JOIN {node} n ON n.nid = i.sid&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;(n.type = 'band')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// return the unchanged match if no node is found (the tags are removed)</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$nd</span> <span style="color: #339933;">=</span> node_load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the node corresponding to the first (best) result</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> l<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'node/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$nd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// create a link</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is one more problem with this: <code>do_search</code> complains if you give it a single keyword that is shorter than 3 characters. It would be nice if this error was supressed and nothing happened, instead:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// search keywords need to be at least 3 characters, so do nothing</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> do_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'node'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;INNER JOIN {node} n ON n.nid = i.sid&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;(n.type = 'band')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// return the unchanged match if no node is found (the tags are removed)</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$nd</span> <span style="color: #339933;">=</span> node_load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the node corresponding to the first (best) result</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> l<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'node/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$nd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// create a link</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All done! Setting up <code>&lt;release&gt;</code> tags for albums works in exactly the same way. Note that using <code>do_search</code> has another advantage in this case: when there are two albums with the same name, the artist can be specified to differentiate between them. This also enables linking to albums with names shorter than 3 characters. To link to the album &#8220;I&#8221; by the band &#8220;Xerath&#8221; (an awesome album by the way <img src='http://benanne.net/code/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ), you can enter <code>&lt;release&gt;Xerath I&lt;/release&gt;</code>. To link to the album &#8220;I&#8221; by the band &#8220;Meshuggah&#8221; (also a brilliant album), enter <code>&lt;release&gt;Meshuggah I&lt;/release&gt;</code>.</p>
<h3>Stuff that I forgot to mention earlier</h3>
<p>To experiment with PHP code using the Drupal API, you can use <a href="http://drupal.org/node/85694">the method described here</a>. In short: enable the <em>PHP filter</em> optional core module, stuff your code into the body field of a new node, set the input format to &#8220;PHP code&#8221; and preview it.</p>
<p>It is also interesting to note that this works great with the <a href="http://drupal.org/project/pathauto"><code>pathauto</code> module</a>; the links inserted are the aliased versions, if they exist. The <code>l</code> function takes care of this, apparently. This means that <code>&lt;band&gt;Meshuggah&lt;/band&gt;</code> will link to <code>http://got-djent.com/band/meshuggah</code>, rather than to <code>http://got-djent.com/node/2</code>. Awesome <img src='http://benanne.net/code/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> .</p>
<p>A downside of this method is that it isn&#8217;t very <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>. The same code has to be added for each content type. I guess it could be interesting to turn this into a full fledged Drupal module, but I don&#8217;t have the time to write and maintain it at the moment. If you think this is useful and would like to implement it, by all means, go ahead.</p>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=408</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing lousy introspection support in Python DB APIs (partially)</title>
		<link>http://benanne.net/code/?p=352</link>
		<comments>http://benanne.net/code/?p=352#comments</comments>
		<pubDate>Wed, 02 Sep 2009 21:50:22 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[introspection]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=352</guid>
		<description><![CDATA[For my internship at Think Wize, I&#8217;ve been working on a custom Django management command that compares the current model definitions (models.py) and the current database schema, and tries to spot the differences. I&#8217;ve called it checkdb. Optionally it can also attempt to fix them, with checkdb --fix.
syncdb is not agile
Django&#8217;s syncdb management command, used [...]]]></description>
			<content:encoded><![CDATA[<p>For my internship at <a href="http://www.think-wize.com/">Think Wize</a>, I&#8217;ve been working on a custom <a href="http://docs.djangoproject.com/en/dev/howto/custom-management-commands/">Django management command</a> that compares the current model definitions (<code>models.py</code>) and the current database schema, and tries to spot the differences. I&#8217;ve called it <code>checkdb</code>. Optionally it can also attempt to fix them, with <code>checkdb --fix</code>.</p>
<h3><code>syncdb</code> is not agile</h3>
<p>Django&#8217;s <code>syncdb</code> management command, used to synchronise the database with the model definitions, is often criticised because it refuses to alter already existing tables. Django&#8217;s development team stands by this. They reason that altering the tables to match changed models is a very complex and ambiguous process, which is too risky to be completely automated. They are probably right. There are so many parameters to take into account. In addition, it is basically impossible to tell whether a field has been changed, or whether a field has been removed and another one added.</p>
<p>This means that, when you change your models during development (and you most likely will, frequently), <code>syncdb</code> is of no use to update your database schema accordingly. <a href="http://docs.djangoproject.com/en/dev/ref/django-admin/#syncdb">Django&#8217;s documentation</a> suggests that you use the <code>sql</code> command, which prints out the SQL that you could use to create the entire database schema matching the current model definitions from scratch. It is then up to you to distill some <code>ALTER TABLE</code> statements out of this.</p>
<p><span id="more-352"></span></p>
<p>What we like so much about web development frameworks is, amongst other things, the abstraction they offer in terms of persistence: <a href="http://en.wikipedia.org/wiki/Object-relational_mapping">Object-relational mapping</a>, or ORM for short. In layman&#8217;s terms, <em>we don&#8217;t have to write SQL queries, the framework writes them for us</em>. We can just write our code in an object oriented fashion, where each entity in the application&#8217;s domain model is represented by a model object. Usually it suffices to call the <code>save</code> method on these objects to persist them. Most of the time this works great. Some ORM toolkits go pretty far in optimising the generated queries, too. But I digress.</p>
<div id="attachment_401" class="wp-caption alignleft" style="width: 210px"><a href="http://benanne.net/code/wp-content/uploads/2009/09/djangologo.png"><img src="http://benanne.net/code/wp-content/uploads/2009/09/djangologo.png" alt="Django - A web development framework" title="djangologo" width="200" height="70" class="size-full wp-image-401" /></a><p class="wp-caption-text">Django - A web development framework</p></div>
<h3>Django has no manners</h3>
<p>All of a sudden, though, Django expects us to write <a href="http://en.wikipedia.org/wiki/Data_Definition_Language">DDL</a> queries. From scratch, pretty much. How rude! It is inevitable that a database schema evolves during the development process, and it probably changes quite often too. We would very much like to avoid having to write queries each time. Many people have tried to find more elegant solutions for this, which gave rise to <em>database migrations</em>.</p>
<h3>Migrations are cool</h3>
<p>In essence, migrations are versioned scripts that define elementary schema changes, preferably one at a time (e.g. &#8220;add the column &#8216;email_address&#8217; to table &#8216;user&#8217;&#8221;). They can be SQL scripts, but very often they are written using an additional abstraction layer (which sits in between SQL and ORM toolkits) that makes them database-agnostic. Preferably, a migration also specifies how to revert the changes it makes. The versioned nature of these scripts makes it possible to quickly bring the database schema up to date, or revert it to an earlier version; in most cases <em>without data loss</em>. They can also be used to recreate the schema elsewhere: in another database, one that is used solely for testing for example, or even on another database engine, if they are database-agnostic.</p>
<p>Ruby on Rails comes with migrations baked in. You don&#8217;t have to use them, but you&#8217;d be crazy not to. It provides a simple API (or DSL, if you&#8217;re one of <em>those</em> guys <img src='http://benanne.net/code/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ) to make schema changes independently of the database engine used. Django doesn&#8217;t come with anything of the sort. There are quite a few plugins that take care of this however, most notably <a href="http://code.google.com/p/django-evolution/">django-evolution</a> and <a href="http://south.aeracode.org/">South</a>. As I have already mentioned in an earlier post, the past few weeks I&#8217;ve been seeing quite a bit of South<a class='footnote' id='note-352-1' href='#footnote-352-1'>1</a>.</p>
<div id="attachment_402" class="wp-caption alignright" style="width: 220px"><a href="http://benanne.net/code/wp-content/uploads/2009/09/southlogo.png"><img src="http://benanne.net/code/wp-content/uploads/2009/09/southlogo.png" alt="South - intelligent schema migrations for Django" title="southlogo" width="210" height="70" class="size-full wp-image-402" /></a><p class="wp-caption-text">South - intelligent schema migrations for Django</p></div>
<h3>South is awesome</h3>
<p>South has one killer feature that sets it apart from most other similar tools: <code>--auto</code>. Whenever you create a migration with South, it &#8220;freezes&#8221; all your model definitions into the migration file. It isn&#8217;t a pretty sight, but it&#8217;s very, very useful; it makes it possible to detect changes to models automatically! This means that in most cases, South can write your migrations for you. All you have to do is review them and correct them if necessary. This holds the middle ground between Django&#8217;s radical <em>&#8220;I ain&#8217;t touchin&#8217; that&#8221;</em> approach and automatically performing all changes. By introducing the possibility to review the changes, you get the best of both worlds.</p>
<h3><code>checkdb --fix</code> is evil</h3>
<p>Automatically performing all changes is basically what <code>checkdb --fix</code>, the script I implemented, does. Usually, this is a very bad idea. In fact, we are already going to use South, so you would think there is no need for such a thing. However, <code>checkdb</code> is intended to be used as a diagnostic tool, and as a &#8220;last resort&#8221; option if migrations don&#8217;t cut it, for whatever reason. One important use case for <code>checkdb</code> is to verify that the database schema is correct  before running any tests. This is, as far as I know, primarily what it will be used for. I used South&#8217;s excellent database API to implement it, in order for it to be as database-agnostic as possible.</p>
<p>Unfortunately, I am not at liberty to release the code into the wild (at least I think I&#8217;m not). I can, however, discuss a an issue I ran into while writing it. One of the biggest problems I encountered was the lousy introspection support in most Python DB API implementations. Database introspection is pretty essential to get a good view of what the schema looks like.</p>
<h3>DB API implementations are lousy</h3>
<p>The Python DB API is a specification that many Python database APIs adhere to, in order to provide uniform access. Alas, once you start writing queries this becomes a problem again, because each database engine has its own dialect of SQL. Anyway, what a DB API implementation should look like is specified in <a href="http://www.python.org/dev/peps/pep-0249/">PEP 249</a>. For introspection purposes, it defines a <code>cursor.description</code> attribute, which describes the structure of the result columns that were returned by the last query performed with the specified cursor. For each column, seven values are supplied: <code>(name, type_code, display_size, internal_size, precision, scale, null_ok)</code>. The values in question are set to <code>None</code> if they are irrelevant for the given column, or if they are <em>not implemented</em>.</p>
<p>The latter seems to happen rather a lot. Sometimes the implementations are even plain wrong, like MySQL&#8217;s, which returns an invalid <code>internal_size</code> for <code>VARCHAR</code> columns. SQLite&#8217;s only implements the first value (<code>name</code>)! postgresql_psycopg2 is actually the &#8220;best&#8221; implementation, but it doesn&#8217;t implement <code>null_ok</code>, which specifies whether the given column is nullable, and which is really quite important for <code>checkdb</code>. Actually, I haven&#8217;t run into any implementations that return a sensible value for <code>null_ok</code>. How hard can it be to make this work? Anyway, this meant I had to find a workaround.</p>
<h3>Standards are useful&#8230;</h3>
<p>While googling, I discovered something called <code>INFORMATION_SCHEMA</code>, which is part of the SQL-92 standard. Basically, it&#8217;s a &#8220;virtual&#8221; database that contains metadata about the databases themselves. This kind of feature is called a <a href="http://en.wikipedia.org/wiki/Database_catalog">database catalog</a>, and most database engines support it. But until recently, each had its own way of structuring this information. MySQL supports <code>INFORMATION_SCHEMA</code> since version 5, PostgreSQL has supported it since version 7.4. <a href="http://www.petefreitag.com/item/666.cfm">There are a couple of others</a>, but these two were the most relevant for my work.</p>
<p>I got kind of lucky, because a fix for MySQL&#8217;s erroneous <code>internal_size</code> had already been written. What I had to implement was <code>null_ok</code>. The solution was to run the following query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> is_nullable <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> table_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'tbl_name'</span> <span style="color: #993333; font-weight: bold;">AND</span> column_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'col_name'</span> <span style="color: #993333; font-weight: bold;">AND</span> table_catalog <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'db_name'</span>;</pre></div></div>

<p>In PostgreSQL, this returns <code>YES</code> or <code>NO</code>, which I then converted to a boolean and inserted in to the tuple returned by <code>cursor.description</code>, for the specified column.</p>
<h3>&#8230;if they are unambiguous</h3>
<p>In MySQL however, <em>this does not work</em>. So much for standards. You have to use this instead:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> is_nullable <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> table_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'tbl_name'</span> <span style="color: #993333; font-weight: bold;">AND</span> column_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'col_name'</span> <span style="color: #993333; font-weight: bold;">AND</span> table_schema <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'db_name'</span>;</pre></div></div>

<p>Note the subtle difference; in MySQL, the database name is stored in <code>table_schema</code>, whereas it is stored in <code>table_catalog</code> in PostgreSQL. Note that both columns exist for both database engines, but the other always contains a worthless value; <code>table_schema</code> is always set to <code>public</code> in PostgreSQL, for example. There is probably a very good reason for this. Right? <img src='http://benanne.net/code/wp-includes/images/smilies/icon_neutral.gif' alt=':-|' class='wp-smiley' /> </p>
<p>I thought I&#8217;d do a write-up of this, because database introspection seems to be neglected very often in Python, both in terms of implementation and documentation. Let me know if it helped you!</p>
<p>On a different note, I fixed a slight cosmetic issue with article titles on this blog. They should no longer overlap with the dates, even if your resolution is lower than 1680&#215;1050 <img src='http://benanne.net/code/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' />
<div class='footnotes'>
<h4>Notes</h4>
<ol class='footnotes'>
<li id='footnote-352-1'><a href='#note-352-1'>&uarr;1</a> While I&#8217;m at it, I should mention that Andrew Godwin, South&#8217;s creator, has been really helpful and patient with me for the past few weeks. Thanks dude! Check out his personal site at <a href="http://www.aeracode.org/">www.aeracode.org</a>. </li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=352</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Apache rewrite rules are almost as horrid as XSLT</title>
		<link>http://benanne.net/code/?p=295</link>
		<comments>http://benanne.net/code/?p=295#comments</comments>
		<pubDate>Thu, 27 Aug 2009 21:14:04 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[rewrite rules]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=295</guid>
		<description><![CDATA[It&#8217;s time for a rant. I haven&#8217;t posted in a while because I haven&#8217;t been doing much that might be of interest to anyone, in terms of coding. Apart from my internship, perhaps; I&#8217;m working at Think Wize, a company based around Django, for six weeks. They are hoping to set up a continuous integration [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time for a rant. I haven&#8217;t posted in a while because I haven&#8217;t been doing much that might be of interest to anyone, in terms of coding. Apart from my internship, perhaps; I&#8217;m working at <a href="http://www.think-wize.com/">Think Wize</a>, a company based around <a href="http://www.djangoproject.com">Django</a>, for six weeks. They are hoping to set up a continuous integration environment of sorts, and I&#8217;ve been given a few tasks to help make this happen. The first few weeks I&#8217;ve mainly been trying to get their codebase to play nice with <a href="http://south.aeracode.org/">South</a>, a schema migration tool for Django. It&#8217;s been quite a challenge. I&#8217;m not sure if I&#8217;m at liberty to say much more about the subject, but that&#8217;s not what this post was going to be about anyway.</p>
<p>Nope, this is a rant. I just spent almost two hours trying to figure out how to get Apache to remove the &#8220;www.&#8221; from URLs. mod_rewrite, goddamn. It isn&#8217;t so much the horrible syntax, or the contradictory information to be found about it, as the fact that it was basically ignoring what I wrote the whole time. Really, really frustrating. I don&#8217;t know why, either. Feel free to continue reading and let me know what I was doing wrong, if you want.</p>
<p><span id="more-295"></span></p>
<p>The reason I&#8217;ve been wasting my time on this is because I&#8217;m going to set up a second website on my VPS: <a href="http://got-djent.com">got-djent.com</a>. It&#8217;s going to be a community portal for people who are interested in djent. Djent is a music thing. Some would argue it is a genre on its own, but I&#8217;m not going to speak out on that. If you&#8217;re into progressive music and/or metal, you might want to check it out. Anyway, I run a fairly large <a href="http://www.last.fm/group/djent">last.fm group called &#8220;djent&#8221;</a>. Recently, we decided that we&#8217;d like to have some more options available than just a shoutbox and a forum with limited functionality, so we&#8217;re going to set up a <a href="http://drupal.org/">Drupal</a>-based community portal. Perhaps I&#8217;ll write more about my Drupal adventures later.</p>
<p>To make this work, I had to set up Apache so there would be two virtual hosts, one for each website. My main site (this one) is the default. I set the <code>ServerName</code> directive to <code>got-djent.com</code> for the other one, and that seems to be working fine now. Everything was fine up to here.</p>
<p>Unfortunately, <a href="http://www.got-djent.com">www.got-djent.com</a> was still pointing to my main site. My guess is that this is because the hostname doesn&#8217;t match the <code>ServerName</code> of the virtual host, so it falls back to the default. Luckily, there is also a <code>ServerAlias</code> directive, so I added <code>ServerAlias www.got-djent.com</code> below that and it worked fine.</p>
<div id="attachment_346" class="wp-caption alignright" style="width: 310px"><a href="http://benanne.net/code/wp-content/uploads/2009/08/cat_keyboard.jpg"><img src="http://benanne.net/code/wp-content/uploads/2009/08/cat_keyboard.jpg" alt="Some keyboards have to endure even worse fates." title="cat_keyboard" width="300" height="230" class="size-full wp-image-346" /></a><p class="wp-caption-text">Some keyboards have to endure even worse fates.</p></div>
<p>However, &#8220;www&#8221; is useless. What is it there for anyway? To remind us that we&#8217;re surfing the World Wide Web? Frankly, I think most internet denizens can tell a URL from an email address these days, without needing the &#8220;www&#8221; prefix to make it blatantly obvious. We would be better off without it. It would save us a lot of keystrokes. And put less strain on our <em>W</em> keys, somehow it seems unfair that they have to endure such ongoing abuse <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>On a more serious note, the prefix affects caching; if you visit <code>http://example.com/foo/bar.html</code>, and then <code>http://<strong>www.</strong>example.com/foo/bar.html</code> later, the entire page will be reloaded (including all images hosted on the same domain) because your browser can&#8217;t tell that the URLs actually point to the same page. This means that it&#8217;s definitely a good idea to make only one of these available. Just serving the user a 404 page on the other is a bit rude though, so the nice thing to do is to install a redirect. A website has been set up to promote this cause: <a href="http://no-www.org/">no-www.org</a><a class='footnote' id='note-295-1' href='#footnote-295-1'>1</a>.</p>
<p>And so, I set out to make my domains <a href="http://no-www.org/faq.php#class_b">class B compliant</a>. I <a href="http://no-www.org/verify.php?u=got-djent.com">got</a> <a href="http://no-www.org/verify.php?u=benanne.net">there</a> eventually, but it was quite a pain. What I needed to do, basically, was throw this into my Apache configuration somewhere:</p>
<pre>RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.got-djent.com$ [NC]
RewriteRule ^(.*)$ http://got-djent.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www.benanne.net$ [NC]
RewriteRule ^(.*)$ http://benanne.net/$1 [R=301,L]</pre>
<p>Someone once said that <a href="http://httpd.apache.org/docs/2.2/rewrite/">mod_rewrite is voodoo</a>. If you think these five lines aren&#8217;t particularly easy on the eyes, you can&#8217;t even begin to fathom what ghastly horrors lurk in the depths of URL rewriting. Seriously, this is nothing. Most of it can obviously be attributed to its extensive use of <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expressions</a>, but even aside from that, it&#8217;s hardly elegant.</p>
<p>Let&#8217;s go through these five lines step by step. What <code>RewriteEngine On</code> does should be fairly obvious. <code>RewriteCond %{HTTP_HOST} ^www.got-djent.com$ [NC]</code> is a bit less transparent. <code>RewriteCond</code> describes a condition for a rewrite rule. Basically, it lets you match a set of variables associated with the incoming request to a regular expression. <code>%{HTTP_HOST}</code> is a variable that contains the host name that was used for the current request<a class='footnote' id='note-295-2' href='#footnote-295-2'>2</a>. This is matched against <code>^www.got-djent.com$</code>, a regular expression which will only match &#8220;www.got-djent.com&#8221; itself. The <code>[NC]</code> is an option flag that indicates the matching should be case insensitive (it stands for <em>no case</em>). A <code>RewriteCond</code> always affects the first <code>RewriteRule</code> that follows it.</p>
<p>Next up is <code>RewriteRule ^(.*)$ http://got-djent.com/$1 [R=301,L]</code>. As you can see, <code>RewriteRule</code> also takes two parameters: a regular expression and a&#8230; well, it&#8217;s a URL of sorts, but it has funny characters like dollar signs in it &#8211; a substitution pattern. The regular expression extracts information from the request URL<a class='footnote' id='note-295-3' href='#footnote-295-3'>3</a> and this is used to interpolate the backreferences (like <code>$1</code>) in the substitution pattern. In this case, we have the regular expression match everything<a class='footnote' id='note-295-4' href='#footnote-295-4'>4</a> and paste this after the domain name <em>without</em> the &#8220;www&#8221;.</p>
<p>This directive also has two option flags. <code>[L]</code> is for <em>last</em>; processing will stop after applying this rule, and any following rules will be ignored. <code>[R]</code> is for <em>redirect</em>, which is what we want to do here. Note that just <code>[R]</code> would send out a <code>302 Moved Temporarily</code> status code. We explicitly specify 301 so it sends out <code>301 Moved Permanently</code>. The last two lines are the same as the previous two, but for my other domain. It is probably possible to consolidate these two into a single rule, but I&#8217;ve had enough of this for a while.</p>
<p>One thing I&#8217;m wondering about is why the dot wildcard (<code>.</code>) usually isn&#8217;t escaped in the regular expression provided to <code>RewriteCond</code>. Out of the stuff I found on Google, roughly half would escape them (<code>www\.got-djent\.com</code>) and the other half wouldn&#8217;t. I left the backslashes out here, because it looks a little bit nicer this way and they don&#8217;t seem to be doing anything. My guess is that they <em>should</em> actually be escaped, but because they are matched against <code>%{HTTP_HOST}</code> they can only ever match a dot anyway. The regular expression itself might also match, say, <code>wwwwgot-djent_com</code>, but that hostname doesn&#8217;t point to my server in the first place. If you have another explanation for this, or if you think mine is incorrect, please let me know in the comments.</p>
<p>Now, my main issue wasn&#8217;t the rules themselves, but where to put them. Nothing seemed to work. I put them in <code>httpd.conf</code>, in <code>sites-available/</code> and in <code>conf.d/</code><a class='footnote' id='note-295-5' href='#footnote-295-5'>5</a>, and threw in some other directives without any luck. The rules just would not get picked up. I know the files in question were all being read though, because I also put <code>RewriteLog "/var/log/rewrite.log"</code> and <code>RewriteLogLevel 9</code> in there for debugging purposes, and these definitely were being picked up. I tested this by browsing the Drupal install I&#8217;m working on, which has some rewrite rules to make its URLs prettier. Those rewrites would show up in the log just fine, but my own rules were doing nothing at all.</p>
<p>The solution, of course, is to put them inside an <code>.htaccess</code> file. However, not just any <code>.htaccess</code> file; it has to be the one <em>in the document root of the default virtual host</em>. There is a simple explanation for this: <code>www.got-djent.com</code> does not match the <code>ServerName</code> (I had obviously removed the <code>ServerAlias</code> directive earlier) of the got-djent.com virtual host, so the request never reaches it. Instead, it goes to the default virtual host. It took me the better part of two hours to finally realise this. Once I knew this, the solution was obvious.</p>
<p>I still don&#8217;t know why placing the directives in any of the main configuration files has no effect. According to Google this should work just fine, and to me it seems like a more natural place to put them in. I hope this post is helpful to anyone who is running into the same kind of problems. For others it will probably be a painfully boring read. I&#8217;m not sorry for this.</p>
<p>To justify the title, I should probably mention that my hate for XSLT is far worse. I respect the power of Apache&#8217;s URL rewrite rules, and the inevitable complexity that comes with this, but XSLT is just a giant sack of unnecessarily complicated shit. Aaaaaaaanyway. I am going to watch an episode of A Bit of Fry and Laurie and then go to bed. I think I&#8217;ve deserved that. Keep an eye out on <a href="http://got-djent.com">got-djent.com</a> if you&#8217;re interested, it&#8217;ll be going live in a few weeks or so. Good night <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
<div class='footnotes'>
<h4>Notes</h4>
<ol class='footnotes'>
<li id='footnote-295-1'><a href='#note-295-1'>&uarr;1</a> Other people think &#8220;www&#8221; is an essential part of internet culture and would rather we used it more liberally: <a href="http://www.www.extra-www.org/">www.www.extra-www.org</a> <img src='http://benanne.net/code/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  . </li>
<li id='footnote-295-2'><a href='#note-295-2'>&uarr;2</a> It <em>doesn&#8217;t</em> contain the full request URL; it seems to be a fairly common mistake to assume that it does. </li>
<li id='footnote-295-3'><a href='#note-295-3'>&uarr;3</a> Note that this URL might have been rewritten by preceding rules. Can you imagine how much fun this must be to debug! </li>
<li id='footnote-295-4'><a href='#note-295-4'>&uarr;4</a> If you don&#8217;t understand why <code>^(.*)$</code> matches everything, pay a visit to <a href="http://www.regular-expressions.info/">www.regular-expressions.info</a>. </li>
<li id='footnote-295-5'><a href='#note-295-5'>&uarr;5</a> I&#8217;m using Ubuntu Server, and its default Apache install has a pretty intricate configuration file structure. </li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=295</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Capturing the mouse with jQuery</title>
		<link>http://benanne.net/code/?p=238</link>
		<comments>http://benanne.net/code/?p=238#comments</comments>
		<pubDate>Tue, 10 Feb 2009 18:46:31 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[client-side]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=238</guid>
		<description><![CDATA[I&#8217;ve been working on a Javascript-driven Django application pretty much non-stop for the last week or so. I&#8217;ll leave my experiences with the Django framework for another post1. There&#8217;s more than twice as much Javascript code in this application than there is Python code, so I can&#8217;t really claim that I know Django yet, to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a Javascript-driven <a href="http://www.djangoproject.com/">Django</a> application pretty much non-stop for the last week or so. I&#8217;ll leave my experiences with the Django framework for another post<a class='footnote' id='note-238-1' href='#footnote-238-1'>1</a>. There&#8217;s more than twice as much Javascript code in this application than there is Python code, so I can&#8217;t really claim that I <em>know</em> Django yet, to be honest.</p>
<p>Anyway, the application I am working on is similar to Google Calendar in terms of user interface. Basically, it allows VTK&#8217;s<a class='footnote' id='note-238-2' href='#footnote-238-2'>2</a> <a href="http://en.wikipedia.org/wiki/Praesidium">praesidium</a> and volunteers to indicate when and how they wish to help for every activity they organise. For each activity, a list of shifts is presented in a timetable. Usability is the main concern here, so everything is mouse-driven and easy to use. At least, that&#8217;s what it is intended to be. There&#8217;s a screenshot <a href="/images/shiften_admin.png">here</a>, which should help to get the general idea. The most interesting part is the administration interface (for the creation of the shift lists), which allows for shifts to be drawn, resized and moved around on the grid with the mouse. <a href="http://jquery.com/">jQuery</a> and its many plugins (<a href="http://ui.jquery.com/">jQuery UI</a> in particular) have made it relatively easy to get this done.</p>
<p>However, there was one thing that bothered me: DOM elements only receive mouse events when the mouse pointer is within their bounds. This is expected behaviour, but sometimes it&#8217;s nice to be able to have a certain element <em>capture</em> the mouse (i.e. keep receiving mouse events as long as a button is pressed, regardless of the position of the pointer), for example when dragging to scroll. Normally, when you accidentally move the mouse pointer outside of the scrollable area, the scrolling will stop because the DOM element is no longer receiving the <em>mousemove</em> event. In addition, this means that the element will never receive the <em>mouseup</em> event. I often perform cleanup actions in a <em>mouseup</em> handler; in this example, the mouse cursor might be changed during dragging, so it has to be reset afterwards. If the event is never triggered, this leaves the application in a &#8220;dirty&#8221; state.</p>
<p><span id="more-238"></span></p>
<p>For my student job last summer, I used Silverlight 2, which allows you to call <code>CaptureMouse</code><a class='footnote' id='note-238-3' href='#footnote-238-3'>3</a> on any interface element. After this, all mouse events will be sent to this element, regardless of where they occur. This behaviour can be removed again with <code>ReleaseMouseCapture</code>. This makes it easy to create user friendly mouse-driven applications; just call the former in the <em>mousedown</em> handler, and the latter on <em>mouseup</em>.</p>
<div id="attachment_249" class="wp-caption alignright" style="width: 310px"><a href="http://benanne.net/code/wp-content/uploads/2009/02/capturedmouse.jpg"><img class="size-full wp-image-249" title="capturedmouse" src="http://benanne.net/code/wp-content/uploads/2009/02/capturedmouse.jpg" alt="A captured mouse." width="300" height="200" /></a><p class="wp-caption-text">A captured mouse.</p></div>
<p>Such a thing, at least to my knowledge, does not exist for web pages. However, this can be circumvented in a number of ways. For starters, it is interesting to note that Firefox<a class='footnote' id='note-238-4' href='#footnote-238-4'>4</a> always executes mouse event handlers bound to the <code>document</code> element when a mouse button is pressed and its window is active, even when the mouse pointer is actually outside of the window boundaries. This is interesting because it allows for Silverlight&#8217;s <code>CaptureMouse</code> behaviour to be mimiced.</p>
<p>Obviously, it makes no sense to bind <em>mousedown</em> handlers to <code>document</code>; the event would always be triggered, and there would be no way to find out which element was actually clicked. However, we could identify the clicked element in the <em>mousedown</em> handler and tuck it away somewhere; the <em>mousemove</em> and <em>mouseup</em> handlers are then able to access this information and realise the desired behaviour, even though they are not bound to the target element.</p>
<p>There are two ways to go about this (at least afaik, there might be more). One is to have each of the <em>mousedown</em> handlers store the element in a variable, which the other handlers check whenever they are invoked (e.g. whenever the corresponding event is triggered on the document element). This variable need not be global; it just has to be part of a scope that is available in all handler functions. You can easily prevent it from being global by wrapping your code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// your code goes here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>An anonymous function is created and it is executed immediately. It serves no purpose except to create a local lexical scope; any variables declared inside the function (using the <code>var</code> keyword) are not available outside it. Note that this practice is recommended to jQuery plugin developers as well. But I digress.</p>
<p>Here&#8217;s an example of how this technique could work:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">/* The code is already wrapped inside a function here, so we need not use
       the previously mentioned technique to protect the &quot;targetElement&quot;
       variable from being exposed globally. */</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> targetElement <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#div1, #div2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mousedown</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> targetElement <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mousemove</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>targetElement.<span style="color: #660066;">id</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;div1&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #006600; font-style: italic;">// handler for #div1</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mousemove</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>targetElement.<span style="color: #660066;">id</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;div2&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #006600; font-style: italic;">// handler for #div2</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// same for mouseup</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The handlers could also be combined into a single one, containing a  (potentially) large <code>if-else</code> structure, or maybe even a <code>switch</code> on <code>targetElement.id</code>. This all looks a bit awkward though, to me at least.</p>
<p>Another way to go about this is by binding the <em>mousemove</em> and <em>mouseup</em> events inside the <em>mousedown</em> handler (and unbinding them again on <em>mouseup</em>). This is the technique I&#8217;m currently using in the application I mentioned earlier. You can define the dynamically attached handlers inside the <em>mousedown</em> handler<a class='footnote' id='note-238-5' href='#footnote-238-5'>5</a>, which gives them access to its scope; this eliminates the need for shared variables.</p>
<p>Here&#8217;s how it looks:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#div1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mousedown</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> moveHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// handler for #div1</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> upHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// handler for #div1</span>
&nbsp;
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mousemove&quot;</span><span style="color: #339933;">,</span> moveHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mouseup&quot;</span><span style="color: #339933;">,</span> upHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mousemove</span><span style="color: #009900;">&#40;</span>moveHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseup</span><span style="color: #009900;">&#40;</span>upHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// same for #div2</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To be honest, I&#8217;m not sure which of these performs best. The first method has the advantage of only requiring binding at the start, whereas the second requires the <em>mousemove</em> and <em>mouseup</em> handlers to be rebound on every <em>mousedown</em> event. For the second method, on the other hand, only a single handler is ever bound to the <em>mousemove</em> and <em>mouseup</em> events (however, this is also true for the first, if all handlers are combined). It does not require any (potentially expensive) &#8220;identity checks&#8221; on the target element, either. Testing the performance of these different techniques would probably yield some interesting results, so I might do that later.</p>
<div id="attachment_281" class="wp-caption alignleft" style="width: 252px"><a href="http://benanne.net/code/wp-content/uploads/2009/02/jquerylogo.png"><img class="size-full wp-image-281" title="jquerylogo" src="http://benanne.net/code/wp-content/uploads/2009/02/jquerylogo.png" alt="jQuery logo" width="242" height="76" /></a><p class="wp-caption-text"> </p></div>
<p>Both of these approaches contain a lot of boilerplate code. It would be nice if we could abstract this into a plugin. Thanks to jQuery&#8217;s extensibility, we can accomplish this fairly easily. I implemented the second of the described techniques, because it was easiest. I&#8217;m not sure how to go about implementing the first, because of the required identity checks.</p>
<p>So here it is:</p>
<ul>
<li><a href="http://benanne.net/misc/jquery-plugins/mousecapture/jquery.mousecapture.js">jquery.mousecapture.js</a> &#8211; the plugin; tested with jQuery 1.3.1, but it should work with older versions.</li>
<li><a href="http://benanne.net/misc/jquery-plugins/mousecapture/demo.html">A simple demo</a> &#8211; try it yourself! Start dragging on the black box, and move the mouse away from it; the <em>move</em> and <em>up</em> handlers will still be executed properly.</li>
</ul>
<p>You use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#test_div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mousecapture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;down&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        s.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> Date.<span style="color: #660066;">now</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mousedown triggered on &quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;move&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mousemove triggered on &quot;</span> <span style="color: #339933;">+</span> Date.<span style="color: #660066;">now</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;capturing since &quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;up&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mouseup triggered on &quot;</span> <span style="color: #339933;">+</span> Date.<span style="color: #660066;">now</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;capturing since &quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Please take in account that I wrote the plugin while writing this article, so I haven&#8217;t tested it in practice yet. I might refactor the shift management application to use it, though. Feel free to suggest improvements, particularly pertaining to the sharing of data between the different handlers. I&#8217;m not too satisfied with how it handles that, but it does the job.</p>
<p><em>Update:</em> it&#8217;s only a few lines of code, but I <a href="http://github.com/benanne/jquery-mouse-capture-plugin/tree/master">threw it on GitHub</a> anyway.
<div class='footnotes'>
<h4>Notes</h4>
<ol class='footnotes'>
<li id='footnote-238-1'><a href='#note-238-1'>&uarr;1</a> Maybe even throw in a few comparisons with Rails&#8230; can you smell the trolls already? </li>
<li id='footnote-238-2'><a href='#note-238-2'>&uarr;2</a> VTK is the fraternity for engineering students at Ghent University. </li>
<li id='footnote-238-3'><a href='#note-238-3'>&uarr;3</a> Check out the <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.capturemouse(VS.95).aspx">MSDN documentation</a> for <code>CaptureMouse</code> for more information. </li>
<li id='footnote-238-4'><a href='#note-238-4'>&uarr;4</a> I haven&#8217;t tested this in other browsers, nor on other operating systems (I use ArchLinux), because I am lazy. For now I&#8217;m assuming that they will exhibit similar behaviour. </li>
<li id='footnote-238-5'><a href='#note-238-5'>&uarr;5</a> I don&#8217;t know if this is a detriment to performance, but it very well might be. </li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=238</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Scoped mixins</title>
		<link>http://benanne.net/code/?p=186</link>
		<comments>http://benanne.net/code/?p=186#comments</comments>
		<pubDate>Thu, 22 Jan 2009 00:00:41 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=186</guid>
		<description><![CDATA[A while ago I read one of Peter Harkins&#8217;s posts about open classes in Ruby, and how insanely awesome and useful they are. As you can probably tell, I wholeheartedly agree. Open classes are just one of those things that make me choose Ruby over pretty much anything else, whenever I get the chance to [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I read <a href="http://push.cx/2009/twitter-and-rubys-open-classes" target="_blank">one of Peter Harkins&#8217;s posts</a> about open classes in Ruby, and how insanely awesome and useful they are. As you can probably tell, I wholeheartedly agree. Open classes are just one of those things that make me choose Ruby over pretty much anything else, whenever I get the chance to do so (though if I&#8217;d have to name the one killer feature, I&#8217;d probably say &#8216;blocks&#8217;).</p>
<p>I vastly prefer this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">rand</span>
    <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC00FF; font-weight:bold;">Kernel</span>::<span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">42</span>, <span style="color:#006666;">24024</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#CC0066; font-weight:bold;">rand</span> <span style="color:#008000; font-style:italic;"># =&gt; 24024</span>
a.<span style="color:#CC0066; font-weight:bold;">rand</span> <span style="color:#008000; font-style:italic;"># =&gt; 42</span></pre></div></div>

<p>to this:</p>
<p><span id="more-186"></span></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> array_rand<span style="color:#006600; font-weight:bold;">&#40;</span>arr<span style="color:#006600; font-weight:bold;">&#41;</span>
  arr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC00FF; font-weight:bold;">Kernel</span>::<span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span>arr.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">42</span>, <span style="color:#006666;">24024</span><span style="color:#006600; font-weight:bold;">&#93;</span>
array_rand<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; 1</span>
array_rand<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; 24024</span></pre></div></div>

<p>Yuck. Looks like PHP<a class='footnote' id='note-186-1' href='#footnote-186-1'>1</a>!</p>
<p>Open classes are also very <em>dangerous</em>. But hey, that&#8217;s Ruby for ya. Ruby trusts programmers to do the right thing, rather than patronise them. If they don&#8217;t, then that&#8217;s their problem, not Ruby&#8217;s. I think that makes a whole lot of sense. In a way it&#8217;s also kind of brave, because Ruby gets a lot of flak for this from people who think it&#8217;s irresponsible to give programmers this kind of power. Good for them, I guess &#8211; I think it&#8217;s rad.</p>
<p>I won&#8217;t go into much detail here about why they could be <em>considered harmful</em>; <a href="http://avdi.org/devblog/2008/02/23/why-monkeypatching-is-destroying-ruby/" target="_blank">enough</a> <a href="http://www.infoq.com/articles/ruby-open-classes-monkeypatching" target="_blank">has already</a> <a href="http://blog.jayfields.com/2008/04/alternatives-for-redefining-methods.html" target="_blank">been written</a> <a href="http://hackety.org/2007/08/10/myCompleteListOfSubstitutePhrasesForTheActWeNowKnowToBeMonkeypatching.html" target="_blank">about that</a>. What it usually boils down to is this: you might be modifying a method in an existing class somewhere. Someone else might be doing this too, while <em>using your code</em>. If he isn&#8217;t aware of what you did, he will expect the method he is modifying to exhibit its usual behaviour. Oops. Debugging this kind of thing is pretty annoying, as you can well imagine.</p>
<p>One could argue that that someone else should have taken the time to get to know your code better before using it. But I think reopening classes (<em>monkey patching</em>, if you will) is so entrenched in Ruby&#8217;s culture that this is not realistic. Rails does it, so it&#8217;s probably okay, right?</p>
<p>Well, in some cases it isn&#8217;t; particularly when other people are going to use your code, e.g. when you&#8217;re writing a library. I use this feature a lot; it keeps my code clean, readable and object oriented. I need <code>Symbol#to_proc</code> and <code>Array#rand</code>! But I don&#8217;t do this when I&#8217;m writing code that other people are just going to use, without taking the time to study its internal workings. Expecting them to do that is unreasonable in most cases. Thank god I don&#8217;t write a lot of library code, because it&#8217;s something I really miss.</p>
<p>Wouldn&#8217;t it be cool if&#8230; we could monkey patch on a per scope basis? We could edit the interfaces of objects we use to our liking, and nobody would ever need to know! I hope I didn&#8217;t get your hopes up with the title of this post, because it can&#8217;t be done. Yet. Well, probably never. I don&#8217;t know <img src='http://benanne.net/code/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Of course, there is <a href="http://www.somethingnimble.com/bliki/mixology">mixology</a>, or the more frivolously named <a href="http://github.com/why/mixico/tree/master">mixico</a>, one of _why&#8217;s creations. These two basically do the same thing; they are C extensions that allow you to <em>mix out</em> a module from a class (e.g. remove it from the inheritance chain). By default, Ruby only allows you to mix them in.</p>
<p>Alas, they are of little use for what I want to achieve, as far as I can tell. What it comes down to is that I want to change the interface of an object within a <strong>static</strong> scope; say, within a module declaration, or maybe within a single source file. You could do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rubygems&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;mixology&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> Translator
  <span style="color:#9966CC; font-weight:bold;">module</span> StringExtensions
    <span style="color:#9966CC; font-weight:bold;">def</span> pig_latin
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot; &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>word<span style="color:#006600; font-weight:bold;">|</span>
        word<span style="color:#006600; font-weight:bold;">&#91;</span>1..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> word<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;ay&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot; &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  str = <span style="color:#996600;">&quot;Hello world&quot;</span>
&nbsp;
  str.<span style="color:#9900CC;">mixin</span> StringExtensions
  <span style="color:#CC0066; font-weight:bold;">puts</span> str.<span style="color:#9900CC;">pig_latin</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;elloHay orldway&quot;</span>
&nbsp;
  str.<span style="color:#9900CC;">unmix</span> StringExtensions
  <span style="color:#CC0066; font-weight:bold;">puts</span> str.<span style="color:#9900CC;">pig_latin</span> <span style="color:#008000; font-style:italic;"># NoMethodError: undefined method `pig_latin' for &quot;Hello world&quot;:String</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>No comments about my <code>pig_latin</code> implementation please, I know it stinks. Just an example.</p>
<p>Anyway, this is worthless. For starters, it looks like crap. It would help if we could do this on a per class basis, instead of per object, but even then it would still be pretty verbose. In fairness, I don&#8217;t really have any ideas how to solve this problem syntactically in an elegant way. But that doesn&#8217;t matter because this doesn&#8217;t do what I want: if my object were to leave the static scope of the <code>Translator</code> module (like when I&#8217;d pass it as a parameter to an external method), it would still include the mixin. Exactly what I&#8217;m trying to avoid.</p>
<p>I guess I could try and write something like this myself but it really sounds like an arduous task, if it is possible at all. For starters, I don&#8217;t know anything about Ruby&#8217;s internal workings, or writing native extensions. My knowledge of C is fairly decent, but I&#8217;m not very fond of the language. In addition, I&#8217;d have to figure out a good solution for syntax. Nope&#8230; i&#8217;m affraid scoped mixins (or maybe <em>mixin closures</em>?) will remain but a dream.</p>
<p>In Ruby at least. Only a few days after I started thinking about this idea, I came across <a href="http://github.com/why/potion/tree/master" target="_blank">_why&#8217;s newest project on GitHub</a>. It&#8217;s a <em>mixin-oriented language</em> called &#8216;Potion&#8217;. Conceptually, it separates behaviour from data, and allows local modifications to the behaviour part of objects: scoped mixins! I haven&#8217;t played with it, but it looks like an interesting experiment, to say the least.</p>
<p><strong>Update</strong>: well what do you know. <a href="http://coderrr.wordpress.com/2008/03/31/solving-the-method-collision-problem-of-monkey-patching/" target="_blank">This</a> seems to do what I described, pretty much. But it looks a bit scary.
<div class='footnotes'>
<h4>Notes</h4>
<ol class='footnotes'>
<li id='footnote-186-1'><a href='#note-186-1'>&uarr;1</a> Okay, so maybe I did that on purpose. Sue me <img src='http://benanne.net/code/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  </li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=186</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Installing Phusion Passenger on Ubuntu 8.04</title>
		<link>http://benanne.net/code/?p=151</link>
		<comments>http://benanne.net/code/?p=151#comments</comments>
		<pubDate>Sat, 03 Jan 2009 22:38:27 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Stuff]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=151</guid>
		<description><![CDATA[There seems to be a bug in passenger-2.0.6 that makes the Apache module installation fail. I was just installing Ruby Enterprise Edition 1.8.6-20081215 and Phusion Passenger (AKA mod_rack or mod_rails), and it bailed out on me saying:
root@evidence:~# /opt/ruby-enterprise-1.8.6-20081215/bin/passenger-install-apache2-module
[...]
Compiling and installing Apache 2 module...
cd /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/passenger-2.0.6
/opt/ruby-enterprise-1.8.6-20081215/bin/ruby -S rake clean apache2
/opt/ruby-enterprise-1.8.6-20081215/bin/ruby: No such file or directory -- rake [...]]]></description>
			<content:encoded><![CDATA[<p>There seems to be a bug in <code>passenger-2.0.6</code> that makes the Apache module installation fail. I was just installing <a href="http://www.rubyenterpriseedition.com/">Ruby Enterprise Edition</a> 1.8.6-20081215 and <a href="http://www.modrails.com/">Phusion Passenger</a> (AKA mod_rack or mod_rails), and it bailed out on me saying:</p>
<pre>root@evidence:~# /opt/ruby-enterprise-1.8.6-20081215/bin/passenger-install-apache2-module
[...]
Compiling and installing Apache 2 module...
cd /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/passenger-2.0.6
/opt/ruby-enterprise-1.8.6-20081215/bin/ruby -S rake clean apache2
/opt/ruby-enterprise-1.8.6-20081215/bin/ruby: No such file or directory -- rake (LoadError)

--------------------------------------------
It looks like something went wrong
[...]</pre>
<p><span id="more-151"></span></p>
<p>Sucks. I was already bothering the guys in #passenger on FreeNode when Google sent me <a href="http://groups.google.com/group/phusion-passenger/browse_thread/thread/e8aff5d45d9fc66c?fwc=1&amp;pli=1">here</a>; apparently this bug is already known and <a href="http://github.com/isaac/passenger/commit/4d8762de82aa4fd5ebc98c27c9cd658a4fcde4a8">a patch</a> seems to have been committed. The problem is that it&#8217;s using the <code>rake</code> that belongs to the primary Ruby installation to get things done, so it only occurs when you have more than one version of Ruby installed. I have MRI Ruby 1.8.6 installed in <code>/usr/bin</code>. I haven&#8217;t used that yet, so I haven&#8217;t bothered to install <code>rake</code> for it either, which explains why the module installer can&#8217;t find <code>/usr/bin/rake</code>.</p>
<p>Most suggestions in the Google Groups thread are pretty useless, but someone suggested</p>
<pre>ln -s /opt/ruby-enterprise-1.8.6-20081215/bin/rake /usr/bin/rake</pre>
<p>I don&#8217;t like symlinks that much, even if they&#8217;re only temporary, so I decided to &#8220;fix&#8221; it this way:</p>
<pre>export PATH=/opt/ruby-enterprise-1.8.6-20081215/bin:$PATH</pre>
<p>&#8230; and then it worked great.</p>
<p>At least, I think so. I still have to edit my Apache configuration and reboot the server. Actually, I don&#8217;t think I have a Rails application around to test it with&#8230; but I plan to fix that, soonish <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Just thought I&#8217;d post this, seeing as other people have had the same problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=151</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Moving out!</title>
		<link>http://benanne.net/code/?p=142</link>
		<comments>http://benanne.net/code/?p=142#comments</comments>
		<pubDate>Sat, 03 Jan 2009 01:45:01 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Stuff]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=142</guid>
		<description><![CDATA[Until today, all my stuff was on a shared host. Didn&#8217;t matter much for this blog, but trying to run Rails applications on prehistoric technology like FastCGI got tiresome quickly. In addition, the server I was hosted on was frequently having issues since a few weeks (at one point, I witnessed it showcasing a load [...]]]></description>
			<content:encoded><![CDATA[<p>Until today, all my stuff was on a shared host. Didn&#8217;t matter much for this blog, but trying to run Rails applications on prehistoric technology like FastCGI got tiresome quickly. In addition, the server I was hosted on was frequently having issues since a few weeks (at one point, I witnessed it showcasing a load average of 240 <img src='http://benanne.net/code/wp-includes/images/smilies/icon_eek.gif' alt='8O' class='wp-smiley' /> something was filling up all memory and swap space, I think). So I decided it was time for me to get myself a <em>proper</em> hosting account: this blog is now hosted on a VPS <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Migrating WordPress blogs sure could be easier. I used <a href="http://yoast.com/move-wordpress-blog-domain-10-steps/">this guide</a>, but I had to change some URLs in the database manually before everything would work properly. Ah well, it could have been worse! I will also try to finish the stylesheet and add some cosmetic improvements in the near future. I don&#8217;t know if anybody has noticed, but the archive pages are mess right now. I also need to tweak things a bit further to balance reasonable performance with memory usage. Apache is such a hog. As a consequence, the server will probably be down occasionally in the next few days. My apologies on beforehand if this bothers anybody.</p>
<p>So yeah. Update your bookmarks (ha ha). <a href="http://code.lemuria-nation.net/">http://code.lemuria-nation.net/</a> is now <a href="http://benanne.net/code/">http://benanne.net/code/</a>! I have installed a redirect at the old location, but I don&#8217;t plan on renewing that domain, so it&#8217;ll be gone in a month or so.</p>
<p>Seeing as this is just a short note to let everybody know about the move, I can throw in some completely unrelated stuff: read <a href="http://abstrusegoose.com/" target="_blank">Abstruse Goose</a>. It&#8217;s awesome! Also check out <a href="http://www.brendangregg.com/specials.html" target="_blank">Brendan Gregg&#8217;s collection of <em>special</em> sysadmin tools</a>!</p>
<p>There, that&#8217;s it. Don&#8217;t worry, my next post will be chock-full of code again <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=142</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>define_method and argument semantics</title>
		<link>http://benanne.net/code/?p=108</link>
		<comments>http://benanne.net/code/?p=108#comments</comments>
		<pubDate>Thu, 01 Jan 2009 22:39:04 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=108</guid>
		<description><![CDATA[define_method is an odd creature. Paul Gross&#8217;s post about its unusual behaviour inspired me to do some research. I even went and browsed through Ruby&#8217;s source  to attempt to understand it, but that turned out to be unnecessary: I reimplemented it in pure1 Ruby. In this post, I&#8217;m going to try and explain why [...]]]></description>
			<content:encoded><![CDATA[<p><code>define_method</code> is an odd creature. <a href="http://www.pgrs.net/2008/12/31/strange-behavior-with-define_method-and-the-wrong-number-of-arguments" target="_blank">Paul Gross&#8217;s post</a> about its unusual behaviour inspired me to do some research. I even went and browsed through Ruby&#8217;s source  to attempt to understand it, but that turned out to be unnecessary: I reimplemented it in pure<a class='footnote' id='note-108-1' href='#footnote-108-1'>1</a> Ruby. In this post, I&#8217;m going to try and explain why <code>define_method</code> behaves the way it does.</p>
<h3>Methods vs. blocks</h3>
<p>Methods and blocks in Ruby are similar things; they are both <em>containers</em> for code, and both of them can accept a number of arguments. On the other hand, the scoping rules and argument semantics for blocks and methods are very, <em>very</em> different.</p>
<p><span id="more-108"></span></p>
<p>Let&#8217;s start with scope. Blocks have access to the local variables of the scope they are created in; this is called <a href="http://en.wikipedia.org/wiki/Static_scoping" target="_blank">static scoping</a>. This implies that they can be used to create <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)" target="_blank">closures</a>. Closures are a very useful construct for callbacks, for example<a class='footnote' id='note-108-2' href='#footnote-108-2'>2</a>. Methods do not have this property.</p>
<p>Another difference is the way they handle arguments. Methods are pretty anal about them; if they expect you to give them three, you&#8217;d better give them three or they will complain by raising an <code>ArgumentError</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># relevant output is in the comments</span>
<span style="color:#9966CC; font-weight:bold;">def</span> foo<span style="color:#006600; font-weight:bold;">&#40;</span>arg<span style="color:#006600; font-weight:bold;">&#41;</span>
  arg
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
foo<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;bar&quot;</span>
foo <span style="color:#008000; font-style:italic;"># ArgumentError: wrong number of arguments (0 for 1)</span>
foo<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;bar&quot;</span>, <span style="color:#996600;">&quot;baz&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># ArgumentError: wrong number of arguments (2 for 1)</span></pre></div></div>

<p>Blocks are a different story. In general, they are a bit looser about accepting arguments they don&#8217;t expect. It depends, however. This is an aspect of Ruby that I&#8217;ve always found very confusing: there are two kinds of argument semantics for blocks. To understand how this is possible, a couple of things have to be explained first.</p>
<h3>Procs vs. lambdas</h3>
<p>There are two ways to create an object that encapsulates a block in Ruby: <code>lambda</code><a class='footnote' id='note-108-3' href='#footnote-108-3'>3</a> or <code>Proc.new</code>. Let&#8217;s call the former type of object a lambda, and the latter a proc<a class='footnote' id='note-108-4' href='#footnote-108-4'>4</a>. Procs and lambdas can be <em>executed</em> by using their <code>call</code> method. Procs and lambdas have different argument semantics, amongst <a href="http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby" target="_blank">a few</a> <a href="http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/" target="_blank">other differences</a>. Let&#8217;s compare them:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">p2 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>foo, bar<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>foo, bar<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
l2 = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>foo, bar<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>foo, bar<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
p2.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span>, <span style="color:#996600;">&quot;bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;]</span>
p2.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, nil]</span>
p2.<span style="color:#9900CC;">call</span> <span style="color:#008000; font-style:italic;"># =&gt; [nil, nil]</span>
p2.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span>, <span style="color:#996600;">&quot;bar&quot;</span>, <span style="color:#996600;">&quot;baz&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;]</span>
&nbsp;
l2.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span>, <span style="color:#996600;">&quot;bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;]</span>
l2.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># ArgumentError: wrong number of arguments (1 for 2)</span>
l2.<span style="color:#9900CC;">call</span> <span style="color:#008000; font-style:italic;"># ArgumentError: wrong number of arguments (0 for 2)</span>
l2.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span>, <span style="color:#996600;">&quot;bar&quot;</span>, <span style="color:#996600;">&quot;baz&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># ArgumentError: wrong number of arguments (3 for 2)</span></pre></div></div>

<p>As you can see, procs really don&#8217;t care about what they&#8217;re given. Missing arguments are replaced with <code>nil</code>, and obsolete arguments are just ignored. Lambdas, on the other hand, seem to be as strict as methods. However, <em>this is only true for lambdas with two or more arguments</em>. Wha? <img src='http://benanne.net/code/wp-includes/images/smilies/icon_eek.gif' alt='8O' class='wp-smiley' /> Yeah, I know.</p>
<h3>It gets worse</h3>
<p>When a proc or lambda is created from a block with 0 or 1 arguments, different rules apply. Let&#8217;s look at a block with no arguments first:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">p0 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;nothing here&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
l0 = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;nothing here&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
p0.<span style="color:#9900CC;">call</span> <span style="color:#008000; font-style:italic;"># =&gt; nil</span>
p0.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; nil</span>
&nbsp;
l0.<span style="color:#9900CC;">call</span> <span style="color:#008000; font-style:italic;"># =&gt; nil</span>
l0.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; nil</span></pre></div></div>

<p>Looks like both of them don&#8217;t really care: all arguments are ignored. This was to be expected for procs, but for lambdas this behaviour is a bit surprising. When our block has a single argument, other mechanisms come into play:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">p1 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>foo<span style="color:#006600; font-weight:bold;">|</span> foo <span style="color:#006600; font-weight:bold;">&#125;</span>
l1 = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>foo<span style="color:#006600; font-weight:bold;">|</span> foo <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
p1.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;foo&quot;</span>
p1.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span>, <span style="color:#996600;">&quot;bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;] # warning: multiple values for a block parameter (2 for 1)</span>
p1.<span style="color:#9900CC;">call</span> <span style="color:#008000; font-style:italic;"># =&gt; nil # warning: multiple values for a block parameter (0 for 1)</span>
&nbsp;
l1.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;foo&quot;</span>
l1.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span>, <span style="color:#996600;">&quot;bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;foo&quot;, &quot;bar&quot;] # warning: multiple values for a block parameter (2 for 1)</span>
l1.<span style="color:#9900CC;">call</span> <span style="color:#008000; font-style:italic;"># =&gt; nil # warning: multiple values for a block parameter (0 for 1)</span></pre></div></div>

<p>Again, lambdas and procs behave identically. When they are called with too many arguments, these are interpreted as an array, and a warning is issued. It&#8217;s almost like the splat operator is implicitly present. What&#8217;s a bit curious is that something similar also happens when no arguments are supplied; except <code>nil</code> is assigned to the argument, rather than <code>[]</code>, which I would have found a more logical choice.</p>
<h3>Strict vs. loose</h3>
<p>Let&#8217;s bring some structure into all of this; we could say that <strong>there are two kinds of argument semantics in Ruby: <em>strict</em> and <em>loose</em></strong>. Methods always have strict semantics. Procs always have loose semantics. <em>Lambdas have loose semantics when they have 0 or 1 arguments, and strict semantics otherwise</em>.</p>
<p>Of course, turning blocks into objects isn&#8217;t something we do very often. At least I don&#8217;t &#8212; <code>yield</code> is so much handier! It looks like using <code>yield</code> and implicit block parameters gives you loose semantics:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;bar&quot;</span>, <span style="color:#996600;">&quot;baz&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>bar, baz<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>bar, baz<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;bar&quot;, &quot;baz&quot;]</span>
run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>bar, baz<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>bar, baz<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;bar&quot;, nil]</span>
run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;bar&quot;</span>, <span style="color:#996600;">&quot;baz&quot;</span>, <span style="color:#996600;">&quot;bananas&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>bar, baz<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>bar, baz<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;bar&quot;, &quot;baz&quot;]</span></pre></div></div>

<p>I can&#8217;t think of any good reasons why these things are the way they are. It&#8217;s quite complicated and not very well documented. If anyone knows any, please don&#8217;t hesitate to let me know.</p>
<h3>Enter <code>define_method</code></h3>
<p><code>define_method</code> lets you define methods dynamically. It turns a block in to a method, pretty much. As if things weren&#8217;t confusing enough! It exhibits some pretty strange behaviour, as mentioned in <a href="http://www.pgrs.net/2008/12/31/strange-behavior-with-define_method-and-the-wrong-number-of-arguments" target="_blank">Paul Gross&#8217;s post</a>. But, knowing what we know now about argument semantics, it looks a lot like <code>define_method</code> turns blocks into lambdas and lets you call them as methods.</p>
<p>First, I just want to illustrate how <code>define_method</code> can indeed be useful because it allows you to make use of static scoping:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  bar = <span style="color:#996600;">&quot;bar&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> get_bar
    bar
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Foo.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">get_bar</span> <span style="color:#008000; font-style:italic;"># NameError: undefined local variable or method `bar'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Foo
  bar = <span style="color:#996600;">&quot;bar&quot;</span>
  define_method <span style="color:#ff3333; font-weight:bold;">:get_bar</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    bar
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Foo.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">get_bar</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;bar&quot;</span></pre></div></div>

<p>I am aware that as examples go, this one is pretty contrived. I&#8217;m sorry, I can&#8217;t think of anything better <img src='http://benanne.net/code/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>I figured out all of this by trying to reimplement <code>define_method</code> in pure Ruby. The whole proc vs. lambda thing was the missing link. Once I realised this, it was pretty straightforward. I&#8217;ll show you the end result, but I&#8217;m warning you, it isn&#8217;t pretty:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  @@blocks = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">my_def</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">&amp;</span>blk<span style="color:#006600; font-weight:bold;">&#41;</span>
    @@blocks<span style="color:#006600; font-weight:bold;">&#91;</span>name<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>blk<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># IMPORTANT!</span>
    <span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;
    def #{name}(*args)
      @@blocks[:#{name}].call(*args)
    end
    &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  my_def <span style="color:#ff3333; font-weight:bold;">:no_args</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;no args&quot;</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  my_def <span style="color:#ff3333; font-weight:bold;">:one_arg</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>one<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> one
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  my_def <span style="color:#ff3333; font-weight:bold;">:two_args</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>one, two<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> one
    <span style="color:#CC0066; font-weight:bold;">p</span> two
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is basically <a href="http://www.pgrs.net/2008/12/31/strange-behavior-with-define_method-and-the-wrong-number-of-arguments" target="_blank">Paul Gross&#8217;s example</a>, but I replaced <code>define_method</code> by my own implementation. I used a class variable hash to store the blocks, because I had no idea how to make them available inside the created methods otherwise. Remember that method definitions are not statically scoped <img src='http://benanne.net/code/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h3>There is hope yet</h3>
<p><a href="http://effectif.com/2008/11/23/notes-from-the-ruby-manor-part-2">It is said</a> that a lot of this has changed in Ruby 1.9. I haven&#8217;t gotten round to testing it yet, but I think I will. I hope they have considerably simplified these things. They should, if they haven&#8217;t.
<div class='footnotes'>
<h4>Notes</h4>
<ol class='footnotes'>
<li id='footnote-108-1'><a href='#note-108-1'>&uarr;1</a> Well, pure&#8230; it isn&#8217;t pretty, but it <em>is</em> Ruby <img src='http://benanne.net/code/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  </li>
<li id='footnote-108-2'><a href='#note-108-2'>&uarr;2</a> I&#8217;m not going to discuss the virtues of closures and static scoping here; enough people have already done that. I&#8217;m sure Google will be glad to help you out if you want to know more! </li>
<li id='footnote-108-3'><a href='#note-108-3'>&uarr;3</a> Note that <code>proc</code> is an alias for <code>lambda</code>, to make things even more confusing. In addition, the created object&#8217;s class is <code>Proc</code> in both cases! </li>
<li id='footnote-108-4'><a href='#note-108-4'>&uarr;4</a> This is my own convention; I&#8217;ve seen other people use these terms in the same fashion, but nobody can stop you from calling a lambda a proc, really. I mean, even Ruby thinks it&#8217;s a <code>Proc</code> <img src='http://benanne.net/code/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  </li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=108</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript arrays smell</title>
		<link>http://benanne.net/code/?p=50</link>
		<comments>http://benanne.net/code/?p=50#comments</comments>
		<pubDate>Sun, 28 Dec 2008 23:35:05 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[client-side]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=50</guid>
		<description><![CDATA[Last night I was writing some Javascript for a Django1 application I&#8217;ve been working on. I was discussing some of Javascript&#8217;s quirks on IRC with someone else who was also scripting.
He wanted to know what happens when you add both values indexed by number, and values indexed by strings (e.g. expando properties &#8211; no, I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I was writing some Javascript for a Django<a class='footnote' id='note-50-1' href='#footnote-50-1'>1</a> application I&#8217;ve been working on. I was discussing some of Javascript&#8217;s quirks on IRC with someone else who was also scripting.</p>
<p>He wanted to know what happens when you add both values indexed by number, and values indexed by strings (e.g. <em>expando</em> properties &#8211; no, <a href="http://redhanded.hobix.com/cult/ahaNoticeTheExpandoWhichPrecludes.html" title="Expando!" target="_blank">I&#8217;m not making this up</a>); particularly how the <code>Array.length</code> property would deal with that. I told him that in Javascript, <code>length</code> always returns the largest integer key value plus one. I&#8217;m not sure if that&#8217;s 100% correct, but that&#8217;s how I remember it, and his tests seemed to confirm this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Relevant output is in the comments.</span>
<span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 1</span>
foo<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;baz&quot;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 1001</span></pre></div></div>

<p><span id="more-50"></span></p>
<p>He also mentioned that when adding more elements to the array with <code>Array.push</code>, assigned keys are also incremented from the largest available key value:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">foo.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 1001</span>
foo.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 1002</span></pre></div></div>

<p>It&#8217;s almost as if:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">push</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I remember doing it like that, before I found out about the existence of <code>push</code>. Yuck. I guess it sorta makes sense for regular arrays, but it&#8217;s still fishy.</p>
<p>Then I started wondering if this behaviour could cause an overflow in an array with only two elements&#8230; because I figured that would be quite hilarious. Say we create an empty array, and insert an element with Javascript&#8217;s largest possible integer value as its key. Then, we <code>push</code> another element onto it. Chaos ensues! Right?</p>
<p>Turns out it&#8217;s not as simple as I first thought: <em>there is no such thing as an &#8220;integer&#8221; in Javascript</em>. <a href="http://bytes.com/groups/javascript/91435-maximum-number" target="_blank" title="Largest possible number in Javascript">All numbers are floats</a>. Sweet Jesus! For those who don&#8217;t know how floats are stored internally: it&#8217;s comparable to <a href="http://en.wikipedia.org/wiki/Scientific_notation">scientific notation</a>. In <code>1.0e20</code>, <code>1.0</code> is the significand or mantissa, and <code>20</code> is the exponent. A floating point number consists of those two parts, represented as integers. Well, in reality it&#8217;s not as simple as that, but it isn&#8217;t far off.</p>
<p>What this implies is that for small numbers, precision is very high. The smaller the exponent<a class='footnote' id='note-50-2' href='#footnote-50-2'>2</a>, the smaller the difference between floats with successive mantissas. For very large numbers, on the other hand, this difference becomes, well, pretty huge. In fact, at some point <em>it becomes larger than one</em>, which is a bit of a bummer when you&#8217;re forced to use them as integers. A demonstration:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">1e300<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 1e+300</span>
1e300 <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 1e+300</span>
1e300 <span style="color: #339933;">==</span> 1e300 <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; true</span></pre></div></div>

<p>Eh?<br />
Javascript <em>truncates</em> floats. It ignores the least significant digits that cannot be represented.</p>
<p>The largest number that can be represented accurately in Javascript is&#8230;<br />
wait for it&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">0xfffffffffffff80000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000</pre></div></div>

<p>&#8230; or about <code>1.798e+308</code>. That&#8217;s 257 digits, not including the hexadecimal prefix <code>0x</code>, in case you were wondering.</p>
<p>However, to make our array go completely bonkers, we don&#8217;t even have to go that far. <code>1e300</code>, which is a bit easier to type, will do just fine. Get a load of this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
foo<span style="color: #009900;">&#91;</span>1e300<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 0</span>
foo.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; -1</span></pre></div></div>

<p>What&#8217;s going on here? As of yet I haven&#8217;t been able to explain this behaviour. Please shed your light on this in a comment if you can figure it out! I would be eternally grateful. It almost seems like the element hasn&#8217;t been added at all! But this shows that it&#8217;s still in there:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> foo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>k <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span> <span style="color: #339933;">+</span> foo<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// displays a dialog box with &quot;1e+300: baz&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So I guess it mimics the behaviour of an expando property; those are also ignored for the calculation of the length property because they are not numbers. Except this one is. Weird.</p>
<p>Let&#8217;s see what happens if we <code>push</code> another element onto the array. Seeing as <code>1e300 + 1 == 1e300</code>, maybe the original element will be overwritten?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">foo.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;baz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 1</span>
foo.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;baz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; 0</span></pre></div></div>

<p>Seriously, wtf?<br />
It looks like the <code>push</code> operation uses the incorrect <code>length</code> property, which is set to <code>0</code>, and concludes that this is the first element to be added to the array. Both elements are still there however, as can be shown by applying the previous <code>for</code> loop on it again.</p>
<p>I guess <code>1e300</code> is a pretty intangible number. &#8220;Nothing to worry about&#8221;, you&#8217;d think, &#8220;why would I want to use numbers that large in any sort of application?&#8221;. It turns out that indices larger than about <code>4e9</code> have this kind of effect. <code>4e9</code> Is only a couple of billion, really. For Javascript it&#8217;s probably a lot, but generally speaking it&#8217;s not <code>that</code> much.</p>
<p>In addition, I found that Javascript can discern between numbers that are only <code>1</code> apart until about <code>9e15</code>, which makes me wonder why this weird behaviour suddenly pops up at about 4 billion. It seems so arbitrary. Anybody have any clues?</p>
<p>I guess the moral of the story is: <em>don&#8217;t do this</em>. Don&#8217;t use large numbers as array keys in Javascript. Also, don&#8217;t place more than 4 billion elements in a single array. Although I don&#8217;t think you were planning on doing that <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Note that all of these experiments were conducted in Firefox 3.0.5 on Linux using <a href="http://getfirebug.com/">Firebug</a>. It would be interesting to see how other browsers respond to this nonsense, but I don&#8217;t have the time or the motivation to test those myself. If you do, please let me know what happens<a class='footnote' id='note-50-3' href='#footnote-50-3'>3</a>.
<div class='footnotes'>
<h4>Notes</h4>
<ol class='footnotes'>
<li id='footnote-50-1'><a href='#note-50-1'>&uarr;1</a> Yes, you read it right, <a title="Django" href="http://www.djangoproject.com/" target="_blank">Django</a>! At VTK, the fraternity for civil engineering students, everyone uses the Django framework for web development. Seeing as I volunteered to write an internal shift management application, I&#8217;m delving into it as well. It&#8217;ll be interesting to see for myself how it compares to Rails <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </li>
<li id='footnote-50-2'><a href='#note-50-2'>&uarr;2</a> by &#8220;smaller&#8221; I mean negative, and large in absolute value. </li>
<li id='footnote-50-3'><a href='#note-50-3'>&uarr;3</a> Maybe something entirely different happens; in that case, please substitute every occurrence of <em>Javascript</em> in this post with <em>Firefox 3.0&#8217;s implementation of Javascript</em>. </li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=50</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Merb is getting merged into Rails 3! Hell yes!</title>
		<link>http://benanne.net/code/?p=10</link>
		<comments>http://benanne.net/code/?p=10#comments</comments>
		<pubDate>Sat, 27 Dec 2008 20:54:49 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Merb]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://benanne.net/code/?p=10</guid>
		<description><![CDATA[Rails is an MVC web development framework for Ruby. Rails is awesome and magical! Rails helps speed up development, and it&#8217;s just a lot of fun to use because it takes care of all the stuff that is annoying about developing web applications. Most of it, anyways.
Merb is an MVC web development framework for Ruby. [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_31" class="wp-caption alignleft" style="width: 88px"><a href="http://benanne.net/code/wp-content/uploads/2008/12/rails.png"><img class="size-full wp-image-31" title="Rails" src="http://benanne.net/code/wp-content/uploads/2008/12/rails.png" alt="Ruby on Rails" width="78" height="100" /></a><p class="wp-caption-text">Ruby on Rails</p></div>
<p><a title="Ruby on Rails" href="http://rubyonrails.org">Rails</a> is an MVC web development framework for Ruby. Rails is awesome and magical! Rails helps speed up development, and it&#8217;s just a lot of fun to use because it takes care of all the stuff that is annoying about developing web applications. Most of it, anyways.</p>
<p><a title="Merb" href="http://merbivore.com/" target="_blank">Merb</a> is an MVC web development framework for Ruby. Merb is probably awesome too, though I wouldn&#8217;t know because I&#8217;ve never really used it. &#8220;Merb&#8221; is a contraction of <a title="Mongrel" href="http://mongrel.rubyforge.org/">Mongrel</a> and <a title="ERb" href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERb</a>, because originally all it did was serve ERb template pages through Mongrel. However, it&#8217;s turned into a lot more than that, and has become somewhat of an alternative to Rails.</p>
<div id="attachment_32" class="wp-caption alignright" style="width: 201px"><a href="http://benanne.net/code/wp-content/uploads/2008/12/merb.png"><img class="size-full wp-image-32" title="Merb" src="http://benanne.net/code/wp-content/uploads/2008/12/merb.png" alt="Merb is Mongrel + ERb" width="191" height="65" /></a><p class="wp-caption-text">Merb is Mongrel + ERb</p></div>
<p>The main difference with Rails is its underlying philosophy: Merb is framework-agnostic, focusses on performance and has a fixed public API. <a title="Rails 2.2 is thread safe" href="http://weblog.rubyonrails.org/2008/11/21/rails-2-2-i18n-http-validators-thread-safety-jruby-1-9-compatibility-docs" target="_blank">Until recently</a>, Merb had the advantage over Rails of being thread safe. Check out <a title="Interview with Yehuda Katz" href="http://www.infoq.com/interviews/katz-merb" target="_blank">this interview with Yehuda Katz</a> if you want to know more about Merb and how it differs from Rails.</p>
<p>Apparently, Merb and Rails are <a href="http://weblog.rubyonrails.org/2008/12/23/merb-gets-merged-into-rails-3">joining forces</a>. It almost sounds like a bad April fools joke, except it&#8217;s Christmas. Merb and Rails are different. In addition, it seemed like their development teams and communities <a title="Rails vs Merb drama" href="http://merbist.com/2008/11/15/rails-vs-merb-drama/" target="_blank">sorta hated each others guts</a> until very recently.</p>
<p><span id="more-10"></span> Rails is a full stack framework: it provides the M, the V and the C in MVC, and more. However, so is Merb. The difference is that Rails is <em>opinionated</em>: you get ActiveRecord to take care of ORM, Prototype to help you write cleaner, meaner javascript. Rails&#8217;s different components fit together like legos.  They were written for eachother, and this <em>synergy</em> allows you to do some pretty cool stuff. Writing your javascript code using Ruby, for example (RJS).</p>
<p>Merb on the other hand is <em>framework-agnostic</em>. You use whatever tools you want to use; as long as someone&#8217;s written an adapter for it, Merb will know how to deal with it. This is why Merb boasts support for ActiveRecord, DataMapper and Sequel (among others), jQuery and Prototype, &#8230; you get the drift.</p>
<p>Using other tools with Rails is definitely possible, but Rails was not built with this in mind, and it shows. You can use your favourite libraries if you want to, but it&#8217;s probably going to leave you with a bitter taste: a lot of handy shortcuts <em>just aren&#8217;t going to work anymore</em>. The synergy that makes Rails so&#8230; well, Rails, fades away. Note that a lot has been done to try and remedy this; take a look at the <a href="http://ennerchi.com/projects/jrails">jRails project</a>, for example. But remedies won&#8217;t do, as far as I&#8217;m concerned <img src='http://benanne.net/code/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>This is just one of the things that&#8217;s made me want to give Merb a try; I happen to be quite an avid fan of jQuery. I&#8217;ve attempted to use it for small projects a couple of times (mind you, by most people&#8217;s standards all of my web development projects are small), but I always ended up going back to Rails because I just didn&#8217;t have the time to learn all the new stuff. Slices? Parts? Say what? I found that Merb and Rails handle things quite differently, even though they&#8217;re both MVC-based. Rails still did a great job of course, despite my pet peeves with it.</p>
<p>I wonder what will happen to Rails when things like framework agnosticism get thrown in the mix. I would love to give DataMapper and jQuery a try, but how will this affect the synergy that makes Rails so productive? Ideally, things like AJAX form helpers would work with any javascript library you can think of, but how realistic is this? Won&#8217;t the necessity to implement the same functionality with so many different backends hold back innovation? I&#8217;m very curious to see how they will handle this kind of problem.</p>
<p>In other completely unrelated news, my apologies for not posting  anything in the past few months. In fairness, I didn&#8217;t really have anything to write about. This doesn&#8217;t seem like the place to discuss school projects (although I did write a pretty cool cache hierarchy simulator in Ruby, maybe I&#8217;ll go into that later, we&#8217;ll see).</p>
]]></content:encoded>
			<wfw:commentRss>http://benanne.net/code/?feed=rss2&amp;p=10</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
