<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>dot.Using</title>
	
	<link>http://blog.kesor.net</link>
	<description>Making technology about computers, and computers about usability.</description>
	<lastBuildDate>Mon, 29 Jun 2009 22:46:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/dotusing" /><feedburner:info uri="dotusing" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>I think I really dislike Python</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/Iw6C-w9i-Pc/</link>
		<comments>http://blog.kesor.net/2009/04/21/i-think-i-really-dislike-python/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 11:49:19 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/?p=32</guid>
		<description><![CDATA[Ruby:
> irb
irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a[10] = 20
=> 20
irb(main):003:0> a
=> [1, 2, 3, nil, nil, nil, nil, nil, nil, nil, 20]


Perl:
> perl
@a = (1,2,3);
$a[10] = 20;
use Data::Dumper;
print Dumper(@a);
$VAR1 = 1;
$VAR2 = 2;
$VAR3 = 3;
$VAR4 = undef;
$VAR5 = undef;
$VAR6 = undef;
$VAR7 = undef;
$VAR8 = undef;
$VAR9 = undef;
$VAR10 = undef;
$VAR11 = 20;


Python:
> python
>>> a [...]]]></description>
			<content:encoded><![CDATA[<h3>Ruby:</h3>
<pre>> irb
irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a[10] = 20
=> 20
irb(main):003:0> a
=> [1, 2, 3, nil, nil, nil, nil, nil, nil, nil, 20]
</pre>
<p><br/></p>
<h3>Perl:</h3>
<pre>> perl
@a = (1,2,3);
$a[10] = 20;
use Data::Dumper;
print Dumper(@a);
$VAR1 = 1;
$VAR2 = 2;
$VAR3 = 3;
$VAR4 = undef;
$VAR5 = undef;
$VAR6 = undef;
$VAR7 = undef;
$VAR8 = undef;
$VAR9 = undef;
$VAR10 = undef;
$VAR11 = 20;
</pre>
<p><br/></p>
<h3>Python:</h3>
<pre>> python
>>> a = [1,2,3]
>>> a[10] = 20
<span style="color:red">Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: list assignment index out of range</span>
>>> a
[1, 2, 3]
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2009/04/21/i-think-i-really-dislike-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2009/04/21/i-think-i-really-dislike-python/</feedburner:origLink></item>
		<item>
		<title>A bright idea in the middle of the day</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/DhR8cBcSXJM/</link>
		<comments>http://blog.kesor.net/2008/12/01/a-bright-idea-in-the-middle-of-the-day/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 10:45:28 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[nagios unix monitoring cruisecontrol automation]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/?p=23</guid>
		<description><![CDATA[I was reading a blog yesterday about &#8220;The sad state of open source monitoring tools&#8221; and was thinking about it for some time. Coincidently today I had a chance to look at my CruiseControl configuration files, which I wrote quite a long time ago.
I really love the DSL that CruiseControl is using for it&#8217;s configuration, [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading a blog yesterday about &#8220;<a href="http://agiletesting.blogspot.com/2008/11/sad-state-of-open-source-monitoring.html">The sad state of open source monitoring tools</a>&#8221; and was thinking about it for some time. Coincidently today I had a chance to look at my <a href="http://cruisecontrol.sf.net">CruiseControl</a> configuration files, which I wrote quite a long time ago.</p>
<p>I really love the DSL that CruiseControl is using for it&#8217;s configuration, it&#8217;s extremely powerful at describing how to build projects. Especially powerful are the variables, that unlike in Ant are not immutable, and the way plugins can be pre-configured with your own defaults, as well as renamed to other names. It&#8217;s really easy to configure it in such a way that adding a new version for a project is just 1-3 lines of XML, for example
<pre>&lt;xxx-project name="XXX v6.66"&gt;
  &lt;property name="version" value="6.66"/&gt;
&lt;/xxx-project&gt;</pre>
<p><br/></p>
<p>Just in those 3 lines, the pre-configuration already includes all the information about the project. Where it is at, who to send e-mail to, where is the version control, EVERYTHING! If the only variable that changes over time is the version number, then that is all you need to leave as a variable &#8230; everything else is just a template that can be re-used. And these templates are extremely easy to combine from smaller templates, it&#8217;s a template-in-the-template kind of configuration.</p>
<p>IMHO this would very much apply to configuration of monitoring software, like <a href="http://www.nagios.org">nagios</a> for example. And the way the (CC) plugins are written in java &#8211; adding new plugins that check all kinds of esoteric things is really easy to do.</p>
<p>If it would also have the XML/XSLT configuration of how the web-interface looks like (the way CruiseControl does), and the super-easy installation (again like in CruiseControl). It would be a really really really great product, extremely powerful, easy to configure, and potentially great looking.</p>
<p>If only <a href="http://www.thoughtworks.com/">ThoughtWorks </a>would write such a thing &#8230; I would be thrilled!</p>
<p>Actually nagios is already extremely similar to what I described, but for some strange reason I find the <em>rigid</em> configuration of nagios a large PITA. Maybe some-day when time stops and I will have unlimited time to code, I will do it myself.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2008/12/01/a-bright-idea-in-the-middle-of-the-day/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2008/12/01/a-bright-idea-in-the-middle-of-the-day/</feedburner:origLink></item>
		<item>
		<title>Testing messy bash scripts</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/_Mol4stDhyc/</link>
		<comments>http://blog.kesor.net/2007/12/22/22/#comments</comments>
		<pubDate>Sat, 22 Dec 2007 00:01:20 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/12/22/22/</guid>
		<description><![CDATA[
Read this book too!
I am reading the book “Refactoring” by Martin Fowler, and just reek with ideas about improving software, as well as solving problems I head-banged during my “software” development career.
On my last job, there was this huge messy heap of bash scripts that were “The Installation” of their main software product. It was [...]]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify">
<div style="float:right;margin-left:2em">Read this book too!<br/><iframe src="http://rcm.amazon.com/e/cm?t=kesornet-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0201485672&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr&#038;npa=1" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></div>
<p>I am reading the book “Refactoring” by Martin Fowler, and just reek with ideas about improving software, as well as solving problems I head-banged during my “software” development career.</p>
<p>On my last job, there was this huge messy heap of bash scripts that were “The Installation” of their main software product. It was a remarkable amount of bad smelling bash code, however it somehow managed to work. My work, at the time, was from-scratch-rewrites of this or that functionality and then somehow plugging it into the existing framework (damn, i call it a framework now).</p>
<p><span id="more-22"></span></p>
<p>It could have been just amazing if I could take the existing <u><a href="http://steve-yegge.blogspot.com/2007/12/codes-worst-enemy.html"><strike>pile of dirt</strike></a></u> <strike>dung</strike> bash scripts &#8211; and refactor it into something that is readable, sort of.</p>
<p>Today, while I was riding the bus, reading “Refactoring” pg.110, it struck me. It’s actually can be extremely easy to <b>test bash scripts!</b> All it takes is a collection of all the familiar commands, like “cat”, “rm”, etc … and one sneaky PATH environment variable. These commands would be fakes, stubs &#8211; they all just print their name and parameters into a log file. In fact, there is just one actual script and the rest are links to that single one.</p>
<p>That log file can be compared before and after a refactoring. It takes less time to output names and parameters to a file than to execute the actual commands. So while running the actual script might take hours, with the stubs it should run in less time, much less. Finally, by comparing your pre and post refactoring log files, you get a really nice test-suite that can help you refactor. I might even call it replacing unreadable code with readable one without breaking much of anything.</p>
<p>In the particular case of the scripts that I’ve mentioned earlier, I guess that the most used refactoring to improve readability would be <i>Inline Method</i>. Whoever wrote the original scripts was very fond of <i>wrappers</i> for commands like &#8220;ln&#8221;. Even though commands like “ln” don’t really need a wrapper. The wrapper with at least two echo commands and a very long name is quite redundant and adds unnecessary complexity.</p>
<p>There are several problems with this simplistic approach though. One of the problems might be that the script is using full path in names (like /bin/ln), instead of relying on the PATH environment variable. Such things can be relatively easily taken care of until the testing solution is perfect. (I guess). One of the things to try, for example, is running bash in restricted mode, if I remember what that is correctly.</p>
<p>I’ll try that on some new messy scripts that I got on my new job!</p>
<p>YEY!
</p></div>
<p>PS: Google docs rocks! (even publish-to-blog kinda works) </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/12/22/22/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/12/22/22/</feedburner:origLink></item>
		<item>
		<title>GMail is broken</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/QMPePfF_Qks/</link>
		<comments>http://blog.kesor.net/2007/11/24/gmail-is-broken/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 09:05:57 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[google]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/11/24/gmail-is-broken/</guid>
		<description><![CDATA[What happened to GMail lately?
It steals focus!
Contacts don&#8217;t work most of the time!
Mail is often not sent because the button pushing has no effect.
Is this GMail 2.0?
Fuck 2.0, I want the working GMail back!
And stop it from stealing focus to it&#8217;s own tab, that is SO annoying.
]]></description>
			<content:encoded><![CDATA[<p>What happened to GMail lately?<br />
It steals focus!<br />
Contacts don&#8217;t work most of the time!<br />
Mail is often not sent because the button pushing has no effect.</p>
<p>Is this GMail 2.0?<br />
Fuck 2.0, I want the working GMail back!<br />
And stop it from stealing focus to it&#8217;s own tab, that is SO annoying.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/11/24/gmail-is-broken/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/11/24/gmail-is-broken/</feedburner:origLink></item>
		<item>
		<title>A small feng shui for the office</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/xf_bBpPiiyc/</link>
		<comments>http://blog.kesor.net/2007/11/19/a-small-feng-shui-for-the-office/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 10:21:13 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[office]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/11/19/a-small-feng-shui-for-the-office/</guid>
		<description><![CDATA[Just bought this today. It&#8217;s compact. Made these nice fire things with a red gift band. If only I could get rid of my room-mate and all his junk, this office could be so much better. It could be perfect.

]]></description>
			<content:encoded><![CDATA[<p>Just bought this today. It&#8217;s compact. Made these nice fire things with a red gift band. If only I could get rid of my room-mate and all his junk, this office could be so much better. It could be perfect.</p>
<p><a href="http://blog.kesor.net/2007/11/19/a-small-feng-shui-for-the-office/lucky-bamboo/" rel="attachment wp-att-18"><img src='http://blog.kesor.net/wp-content/uploads/2007/11/dsc00011_resized.jpg' title='Lucky Bamboo' alt='Lucky Bamboo' /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/11/19/a-small-feng-shui-for-the-office/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/11/19/a-small-feng-shui-for-the-office/</feedburner:origLink></item>
		<item>
		<title>The Motherload of all switches</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/7J2Txy6dEAQ/</link>
		<comments>http://blog.kesor.net/2007/11/19/the-motherload-of-all-switches/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 06:09:43 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/11/19/the-motherload-of-all-switches/</guid>
		<description><![CDATA[When I read a news article with something like this in it:
Thankfully, Sun realizes that not everyone is looking for a 3,456 port InfiniBand switch. So, it has prepped a 72-port InfiniBand switch that will sit on top of the Sun Blade 6048 rack.
I just can&#8217;t stop being amazed.

]]></description>
			<content:encoded><![CDATA[<p>When I read a news article with something like this in it:</p>
<p><a href="http://www.theregister.co.uk/2007/11/19/sun_storage_sc07/"><q>Thankfully, Sun realizes that not everyone is looking for a 3,456 port InfiniBand switch. So, it has prepped a 72-port InfiniBand switch that will sit on top of the Sun Blade 6048 rack.</q></a></p>
<p>I just can&#8217;t stop being amazed.</p>
<p><img src="http://regmedia.co.uk/2007/11/19/sunmagnum.jpg"/></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/11/19/the-motherload-of-all-switches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/11/19/the-motherload-of-all-switches/</feedburner:origLink></item>
		<item>
		<title>Invite the world to GMail</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/9ZUI22H0Qwk/</link>
		<comments>http://blog.kesor.net/2007/09/01/invite-the-world-to-gmail/#comments</comments>
		<pubDate>Sat, 01 Sep 2007 18:28:20 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[google]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/09/01/invite-the-world-to-gmail/</guid>
		<description><![CDATA[The other day I noticed that I have this &#8220;Invite a friend&#8221; on my GMail. I didn&#8217;t use that thing for more than a year, so I took the chance of this re-discovery to get rid of it. Within several minutes I sent 98 invitations to an imaginary friend whose mail bounced at some noreply@somewhere&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I noticed that I have this &#8220;Invite a friend&#8221; on my GMail. I didn&#8217;t use that thing for more than a year, so I took the chance of this re-discovery to get rid of it. Within several minutes I sent 98 invitations to an imaginary friend whose mail bounced at some noreply@somewhere&#8230; address. And it worked &#8211; I got rid of that little blue box that serves no purpose on my GMail page!</p>
<p>Imagine my surprise when today I find that box at the same place, with 50 new invitations to give away.</p>
<p>No, really &#8211; I don&#8217;t have any friends who don&#8217;t have a GMail account. I don&#8217;t want to use this &#8220;feature&#8221;, ever. And it does not contribute a thing to my GMail experience &#8211; get rid of it Google! Put some AdSense there or something.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/09/01/invite-the-world-to-gmail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/09/01/invite-the-world-to-gmail/</feedburner:origLink></item>
		<item>
		<title>JRuby in a JAR</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/_qnc0BtMmLY/</link>
		<comments>http://blog.kesor.net/2007/08/14/jruby-in-a-jar/#comments</comments>
		<pubDate>Tue, 14 Aug 2007 02:05:22 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/08/14/jruby-in-a-jar/</guid>
		<description><![CDATA[A bit of fairy dust, a sprinkle of elven blood, lots of water and boil it all for 5 hours on a low fire. What you get? &#8230; I wouldn&#8217;t know, but I do know that it took me almost 2 whole days to grasp the idea of a JRuby in a JAR, with two [...]]]></description>
			<content:encoded><![CDATA[<p>A bit of fairy dust, a sprinkle of elven blood, lots of water and boil it all for 5 hours on a low fire. What you get? &#8230; I wouldn&#8217;t know, but I do know that it took me almost 2 whole days to grasp the idea of a JRuby in a JAR, with two bootstrappers!</p>
<p>Let&#8217;s assume that I wrote this great utility that does this magnificent function that will help mankind. And this utility/software needs a computer to run. Now, I speak Ruby quite well &#8211; and specify my behavior thoroughly with RSpec. But alas &#8211; most computers don&#8217;t have ruby installed (Java JRE yes, Ruby no, especially in enterprises).</p>
<p>So let&#8217;s also assume that some giants wrote a Ruby interpreter that can run on a JVM, and called it JRuby. And further more assume that most java &#8220;utilities&#8221; can be written inside a single jar, and can be executed with:</p>
<pre lang="shell">java -jar fairy_dust.jar
</pre>
<p><br/><br />
How can I take my ruby code and have it run just like the above? With just a single file and all &#8230;</p>
<p>Answer:<br />
1. <a href="http://jruby.org/">JRuby</a><br />
2. <a href="http://one-jar.sf.net">One-jar</a><br />
3. <a href="http://www.headius.com/jrubywiki/index.php/Java_Integration">Custom Bootstrapper</a></p>
<p>And the result is &#8211; a JAR file, with multiple Ruby files inside &#8211; that works just like a java jar:</p>
<pre lang="shell">java -jar rubified.jar
</pre>
<p><br/><br />
And you can get all the above (with the ant build.xml) for the price of one download!</p>
<p>Presenting: <b><a href='http://blog.kesor.net/wp-content/uploads/2007/08/rubified-01.zip' title='Rubified 0.1 BETA'>Rubified 0.1 BETA</a></b></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/08/14/jruby-in-a-jar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/08/14/jruby-in-a-jar/</feedburner:origLink></item>
		<item>
		<title>Install RubyGems from irb</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/S16pHfbkmGU/</link>
		<comments>http://blog.kesor.net/2007/08/13/install-rubygems-from-irb/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 12:18:02 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[gems]]></category>
		<category><![CDATA[irb]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/08/13/install-rubygems-from-irb/</guid>
		<description><![CDATA[I started using NetBeans recently. And find it&#8217;s ruby support superb! (as far as an IDE can go). But there was an error installing gems with their gui manager, and I was wondering how to debug that.
A quick peek into ruby/bin/gem revealed much of what I needed to know.
Lets demonstrate :


Welcome to the JRuby IRB [...]]]></description>
			<content:encoded><![CDATA[<p>I started using NetBeans recently. And find it&#8217;s ruby support superb! (as far as an IDE can go). But there was an error installing gems with their gui manager, and I was wondering how to debug that.</p>
<p>A quick peek into ruby/bin/gem revealed much of what I needed to know.</p>
<p>Lets demonstrate :<br />
<span id="more-11"></span></p>
<pre lang="ruby">
Welcome to the JRuby IRB Console 

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> Gem.manage_gems
=> true
irb(main):003:0> gem = Gem::GemRunner.new
=> #<Gem::GemRunner:...>
irb(main):004:0> gem.run(['help','commands'])
GEM commands are:

    build            Build a gem from a gemspec
    cert             Adjust RubyGems certificate settings
    check            Check installed gems
    ...
=> nil
irb(main):005:0>
</pre>
<p></p>
<p><b>update:</b> The bug that prevented me from installing gems, turned out to be a _gemrc file that had http_proxy configured. A proxy no longer in use, but since I didn&#8217;t use gems all that much on this workstation before &#8211; I forgot all about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/08/13/install-rubygems-from-irb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/08/13/install-rubygems-from-irb/</feedburner:origLink></item>
		<item>
		<title>HAML Caching CGI</title>
		<link>http://feedproxy.google.com/~r/dotusing/~3/NZ7GewLCdMs/</link>
		<comments>http://blog.kesor.net/2007/07/30/haml-caching-cgi/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 08:38:23 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[haml]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/07/30/haml-caching-cgi/</guid>
		<description><![CDATA[Mike Zillion asked about how to make HAML a processor (of haml files) for Apache on the HAML Group on Google. That inspired me to write a proper wrapper with caching that will Hamlize templates into HTML and cache those for speedy access on subsequent requests.
This is what I came up with:


#!/bin/env ruby

exit if ARGV[0].nil?
exit [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mikezillion.com">Mike Zillion</a> asked about how to make HAML a processor (of haml files) for Apache on the <a href="http://groups.google.com/group/haml/t/b2fd4dbc5f76cd61">HAML Group</a> on Google. That inspired me to write a proper wrapper with caching that will Hamlize templates into HTML and cache those for speedy access on subsequent requests.</p>
<p>This is what I came up with:<br />
<span id="more-9"></span></p>
<pre lang="ruby">
#!/bin/env ruby

exit if ARGV[0].nil?
exit unless File.exists?(ARGV[0])

CACHE_DIR_NAME='cache'

haml_file = ARGV[0]
haml_time = File.stat(haml_file).mtime

html_file = CACHE_DIR_NAME + '/' + haml_file.sub(/aml$/,'tml')
if File.exists?(html_file)
  html_time = File.stat(html_file).mtime

  if html_time > haml_time
    output = File.read(html_file)
  end
end

if output.nil?
  require 'rubygems'
  require 'haml'
  template = File.read(haml_file)
  haml_engine = Haml::Engine.new(template)
  output = haml_engine.to_html()

  # cache the output
  Dir.mkdir(CACHE_DIR_NAME) unless File.directory?(CACHE_DIR_NAME)
  html_file_io = File.open(html_file,"w")
  html_file_io.print(output)
  File.utime(Time.now, haml_time, html_file)
end

# unbuffer output
$stdout.sync = true

require 'cgi'
ENV['SERVER_SOFTWARE'] ||= 'not set'
cgi = CGI.new('html3')
print cgi.header(
        'type'     => 'text/html',
        'charset'  => 'UTF-8',
        'length'   => output.length,
        'server'   => ENV['SERVER_SOFTWARE'],
        'expires'  => Time.now + 10*3600*24, # 10 days
        'Pragma'   => 'no-cache',
        'Last-Modified' => haml_time,
        'Cache-Control' => 'no-cache'
)
print output
</pre>
<p>And as Mike suggested, adding a couple lines to your Apache configuration makes all the difference:</p>
<pre line="1">
  AddType text/haml .haml
  AddHandler haml-file .haml
  Action haml-file /dev/bin/haml_cache_cgi.rb
  Action text/haml /dev/bin/haml_cache_cgi.rb
</pre>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/07/30/haml-caching-cgi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://blog.kesor.net/2007/07/30/haml-caching-cgi/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 0.428 seconds -->
