<?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>Moserei &#187; programming</title>
	<atom:link href="http://moserei.de/index.php/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://moserei.de</link>
	<description></description>
	<lastBuildDate>Tue, 06 Jul 2010 16:18:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Difference between do-end and {} blocks</title>
		<link>http://moserei.de/index.php/190/difference-between-do-end-and-curly-blocks</link>
		<comments>http://moserei.de/index.php/190/difference-between-do-end-and-curly-blocks#comments</comments>
		<pubDate>Sun, 07 Feb 2010 12:44:41 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[block precedence]]></category>
		<category><![CDATA[curly]]></category>
		<category><![CDATA[do end]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=190</guid>
		<description><![CDATA[There is a important difference between the two block syntaxes in Ruby: Their precedence.
Consider following code:

def method1&#40;*args&#41;
  puts &#34;method1 got a block&#34; if block_given?
end
&#160;
def method2&#40;*args&#41;
  puts &#34;method2 got a block&#34; if block_given?
end
&#160;
method1 method2 do
end
&#160;
method1 method2 &#123;
&#125;

You would expect both method calls to produce the same result, wouldn&#8217;t you?
Output:

method1 got a block
method2 got a [...]]]></description>
			<content:encoded><![CDATA[<p>There is a important difference between the two block syntaxes in Ruby: Their precedence.<br />
Consider following code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> method1<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:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;method1 got a block&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> method2<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:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;method2 got a block&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
method1 method2 <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
method1 method2 <span style="color:#006600; font-weight:bold;">&#123;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>You would expect both method calls to produce the same result, wouldn&#8217;t you?<br />
Output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">method1 got a block
method2 got a block</pre></div></div>

<p>Obviously the do-end block is passed to the first method in the expression while the {} block is passed to the method called directly before it.<br />
Another example shows that the assignment does not count as a method call here:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#[...]</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> bar=<span style="color:#006600; font-weight:bold;">&#40;</span>o<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Foo#bar= got a block&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
foo = Foo.<span style="color:#9900CC;">new</span>
&nbsp;
foo.<span style="color:#9900CC;">bar</span> = method1 method2 <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#9966CC; font-weight:bold;">end</span> 
<span style="color:#008000; font-style:italic;">#method1 got a block</span>
&nbsp;
foo.<span style="color:#9900CC;">bar</span> = method1 method2 <span style="color:#006600; font-weight:bold;">&#123;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span> 
<span style="color:#008000; font-style:italic;">#method2 got a block</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/190/difference-between-do-end-and-curly-blocks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing DeviceKit with DBus and Python</title>
		<link>http://moserei.de/index.php/179/accessing-devicekit-with-dbus-and-python</link>
		<comments>http://moserei.de/index.php/179/accessing-devicekit-with-dbus-and-python#comments</comments>
		<pubDate>Fri, 08 Jan 2010 01:23:44 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[dbus]]></category>
		<category><![CDATA[devicekit]]></category>
		<category><![CDATA[devicekit-disks]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=179</guid>
		<description><![CDATA[Under the impression of loosing some really important data because of a damaged partition table on a USB flash drive I am developing a backup strategy for my system.
I know myself and so I decided that I need to automate this. While trying to figure out how one can run a script as soon as [...]]]></description>
			<content:encoded><![CDATA[<p>Under the impression of loosing some really important data because of a damaged partition table on a USB flash drive I am developing a backup strategy for my system.<br />
I know myself and so I decided that I need to automate this. While trying to figure out how one can run a script as soon as a specific drive is mounted, I came across <a href="http://en.wikipedia.org/wiki/DeviceKit">DeviceKit</a>. It&#8217;s the planned <a href="http://www.freedesktop.org/wiki/Software/hal">replacement of HAL</a> and is used in Ubuntu Karmic. Udev is not an option for me, because I don&#8217;t want to mess around with mounting myself (and I hate running stuff as root&#8230;).<br />
So here is what I found out about using DBus and DeviceKit in Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> dbus
&nbsp;
bus = dbus.<span style="color: black;">SystemBus</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
proxy = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;org.freedesktop.DeviceKit.Disks&quot;</span>, 
                       <span style="color: #483d8b;">&quot;/org/freedesktop/DeviceKit/Disks&quot;</span><span style="color: black;">&#41;</span>
iface = dbus.<span style="color: black;">Interface</span><span style="color: black;">&#40;</span>proxy, <span style="color: #483d8b;">&quot;org.freedesktop.DeviceKit.Disks&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#enumerates all devices</span>
<span style="color: #ff7700;font-weight:bold;">print</span> iface.<span style="color: black;">EnumerateDevices</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#gets the device kit path of a specific device</span>
path = iface.<span style="color: black;">FindDeviceByDeviceFile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/dev/sdc1&quot;</span><span style="color: black;">&#41;</span> 
<span style="color: #808080; font-style: italic;">#= &quot;/org/freedesktop/DeviceKit/Disks/devices/sdc&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#gets an object representing the device specified by the path</span>
device = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;org.freedesktop.DeviceKit.Disks&quot;</span>, path<span style="color: black;">&#41;</span> 
&nbsp;
<span style="color: #808080; font-style: italic;">#prints some XML that shows you the available methods, signals and properties</span>
<span style="color: #ff7700;font-weight:bold;">print</span> device.<span style="color: black;">Introspect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#gets a proxy for getting properties</span>
device_prop = dbus.<span style="color: black;">Interface</span><span style="color: black;">&#40;</span>device, <span style="color: #483d8b;">&quot;org.freedesktop.DBus.Properties&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#you need to specify an interface (properties could be ambiguous)</span>
<span style="color: #ff7700;font-weight:bold;">print</span> device_prop.<span style="color: black;">Get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;org.freedesktop.DeviceKit.Disks.Device&quot;</span>, <span style="color: #483d8b;">&quot;device-mount-paths&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#gets a proxy you can call methods on</span>
device_iface = dbus.<span style="color: black;">Interface</span><span style="color: black;">&#40;</span>device, <span style="color: #483d8b;">&quot;org.freedesktop.DeviceKit.Disks.Device&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#unmounts the partition</span>
device_iface.<span style="color: black;">FilesystemUnmount</span><span style="color: black;">&#40;</span>dbus.<span style="color: black;">Array</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'s'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The DBus API of DeviceKit is documented <a href="http://hal.freedesktop.org/docs/DeviceKit-disks/ref-dbus.html">here</a>.<br />
Now we want to be notified when a drive is mounted:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> dbus
<span style="color: #ff7700;font-weight:bold;">import</span> gobject
<span style="color: #ff7700;font-weight:bold;">from</span> dbus.<span style="color: black;">mainloop</span>.<span style="color: black;">glib</span> <span style="color: #ff7700;font-weight:bold;">import</span> DBusGMainLoop
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> device_added_callback<span style="color: black;">&#40;</span>device<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Device %s was added'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>device<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> device_changed_callback<span style="color: black;">&#40;</span>device<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Device %s was changed'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>device<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#must be done before connecting to DBus</span>
DBusGMainLoop<span style="color: black;">&#40;</span>set_as_default=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
bus = dbus.<span style="color: black;">SystemBus</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
proxy = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;org.freedesktop.DeviceKit.Disks&quot;</span>, 
                       <span style="color: #483d8b;">&quot;/org/freedesktop/DeviceKit/Disks&quot;</span><span style="color: black;">&#41;</span>
iface = dbus.<span style="color: black;">Interface</span><span style="color: black;">&#40;</span>proxy, <span style="color: #483d8b;">&quot;org.freedesktop.DeviceKit.Disks&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#addes two signal listeners</span>
iface.<span style="color: black;">connect_to_signal</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'DeviceAdded'</span>, device_added_callback<span style="color: black;">&#41;</span>
iface.<span style="color: black;">connect_to_signal</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'DeviceChanged'</span>, device_changed_callback<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#start the main loop</span>
mainloop = gobject.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
mainloop.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A typical output when a flash drive is plugged in looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Device <span style="color: #000000; font-weight: bold;">/</span>org<span style="color: #000000; font-weight: bold;">/</span>freedesktop<span style="color: #000000; font-weight: bold;">/</span>DeviceKit<span style="color: #000000; font-weight: bold;">/</span>Disks<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>sdc was added
Device <span style="color: #000000; font-weight: bold;">/</span>org<span style="color: #000000; font-weight: bold;">/</span>freedesktop<span style="color: #000000; font-weight: bold;">/</span>DeviceKit<span style="color: #000000; font-weight: bold;">/</span>Disks<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>sdc1 was added
Device <span style="color: #000000; font-weight: bold;">/</span>org<span style="color: #000000; font-weight: bold;">/</span>freedesktop<span style="color: #000000; font-weight: bold;">/</span>DeviceKit<span style="color: #000000; font-weight: bold;">/</span>Disks<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>sdc1 was changed</pre></div></div>

<p>With this knowledge I&#8217;m currently working on a little python script that runs in background and executes a shell script when a file system is mounted. I&#8217;ll post it, when it&#8217;s finished <img src='http://moserei.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/179/accessing-devicekit-with-dbus-and-python/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sequel single table inheritance</title>
		<link>http://moserei.de/index.php/164/sequel-single-table-inheritance</link>
		<comments>http://moserei.de/index.php/164/sequel-single-table-inheritance#comments</comments>
		<pubDate>Tue, 20 Oct 2009 22:08:24 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[sequel]]></category>
		<category><![CDATA[single table inheritance]]></category>
		<category><![CDATA[sti]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=164</guid>
		<description><![CDATA[Sequel&#8217;s STI is not so well documented as ActiveRecord&#8217;s is. Because it cost me about 20 minutes to find out about it&#8217;s syntax, I post a little example.

class A &#60; Sequel::Model
  plugin :single_table_inheritance, :object_type
end
&#160;
class B &#60; A
end

The second argument is the name of the column to be used to store the class name in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sequel.rubyforge.org/">Sequel</a>&#8217;s STI is not so well documented as ActiveRecord&#8217;s is. Because it cost me about 20 minutes to find out about it&#8217;s syntax, I post a little example.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> A <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Sequel::Model</span>
  plugin <span style="color:#ff3333; font-weight:bold;">:single_table_inheritance</span>, <span style="color:#ff3333; font-weight:bold;">:object_type</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> B <span style="color:#006600; font-weight:bold;">&lt;</span> A
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The second argument is the name of the column to be used to store the class name in DB. Don&#8217;t use &#8216;type&#8217; here, it collides with Ruby&#8217;s Object#type.<br />
This works with version 3.5.0 and should work with any version >= 2.12.0.</p>
]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/164/sequel-single-table-inheritance/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Monkeybars &#8211; Little bug</title>
		<link>http://moserei.de/index.php/149/monkeybars-little-bug</link>
		<comments>http://moserei.de/index.php/149/monkeybars-little-bug#comments</comments>
		<pubDate>Thu, 10 Sep 2009 19:31:22 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[monkeybars]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=149</guid>
		<description><![CDATA[For a little side project I am currently evaluating different ways of GUI programming using Ruby. Monkeybars is one of the most interesting candidates. There is a really good article about it, if you want to learn more.
Version 1.0.4 has a little, but annoying bug: If you generate a new application skeleton it won&#8217;t compile/run:

manifest.rb:32:in [...]]]></description>
			<content:encoded><![CDATA[<p>For a little side project I am currently evaluating different ways of GUI programming using Ruby. <a href="http://monkeybars.rubyforge.org/">Monkeybars</a> is one of the most interesting candidates. There is a really good <a href="http://www.ibm.com/developerworks/library/j-monkeybars/index.html">article</a> about it, if you want to learn more.<br />
Version 1.0.4 has a little, but annoying bug: If you generate a new application skeleton it won&#8217;t compile/run:</p>
<pre>
manifest.rb:32:in `require': no such file to load -- monkeybars (LoadError)
	from manifest.rb:32
	from manifest.rb:21:in `require'
	from main.rb:21
</pre>
<p>To fix it, you got to change line 21 of manifest.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">add_to_classpath <span style="color:#996600;">'../lib/java/monkeybars-1.0.2.jar'</span>
<span style="color:#008000; font-style:italic;">#to</span>
add_to_classpath <span style="color:#996600;">'../lib/java/monkeybars-1.0.4.jar'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/149/monkeybars-little-bug/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ActiveRecord: write_attribute and UTC conversion</title>
		<link>http://moserei.de/index.php/113/activerecord-write_attribute-and-utc-conversion</link>
		<comments>http://moserei.de/index.php/113/activerecord-write_attribute-and-utc-conversion#comments</comments>
		<pubDate>Tue, 23 Jun 2009 12:28:46 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[utc]]></category>
		<category><![CDATA[write_attribute]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=113</guid>
		<description><![CDATA[A little gotcha with custom setters for datetime attributes is, that when setting an attribute through &#8216;write_attribute&#8217; it is not converted to UTC (or whatever else your default time zone is).
This problem can easily be reproduced:

./script/generate model Thing a:datetime b:datetime

My model:

class Thing &#60; ActiveRecord::Base
  def b=&#40;d&#41;
    write_attribute&#40;:b, d&#41;
  end
end

My tests:

require [...]]]></description>
			<content:encoded><![CDATA[<p>A little gotcha with custom setters for datetime attributes is, that when setting an attribute through &#8216;write_attribute&#8217; it is not converted to UTC (or whatever else your default time zone is).<br />
This problem can easily be reproduced:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate model Thing a:datetime b:datetime</pre></div></div>

<p>My model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Thing <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> b=<span style="color:#006600; font-weight:bold;">&#40;</span>d<span style="color:#006600; font-weight:bold;">&#41;</span>
    write_attribute<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:b</span>, d<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></pre></div></div>

<p>My tests:</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;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ThingTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  test <span style="color:#996600;">&quot;a and b&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    d = <span style="color:#CC00FF; font-weight:bold;">DateTime</span>.<span style="color:#9900CC;">now</span>
    t = Thing.<span style="color:#9900CC;">new</span>
    t.<span style="color:#9900CC;">a</span> = d
    t.<span style="color:#9900CC;">b</span> = d
    t.<span style="color:#9900CC;">save</span>
    assert_equal t.<span style="color:#9900CC;">a</span>, t.<span style="color:#9900CC;">b</span>
    t.<span style="color:#9900CC;">reload</span>
    assert_equal t.<span style="color:#9900CC;">a</span>, t.<span style="color:#9900CC;">b</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The second assertion will fail. Is this intended or a bug?<br />
Quick fix:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> b=<span style="color:#006600; font-weight:bold;">&#40;</span>d<span style="color:#006600; font-weight:bold;">&#41;</span>
    d = d.<span style="color:#9900CC;">utc</span>
    write_attribute<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:b</span>, d<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/113/activerecord-write_attribute-and-utc-conversion/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery, jRails and the Accept header</title>
		<link>http://moserei.de/index.php/103/jquery-jrails-and-accept-header</link>
		<comments>http://moserei.de/index.php/103/jquery-jrails-and-accept-header#comments</comments>
		<pubDate>Thu, 18 Jun 2009 09:09:12 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[accept header]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jrails]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=103</guid>
		<description><![CDATA[I included jQuery by installing the jRails plugin, which seems nice because you can continue using rails&#8217; ajax helpers. But when I tried to implement a more special functionality by using the $.ajax function it proved impossible to set the Accept header for my request. Neither using the dataType option nor by setting it directly [...]]]></description>
			<content:encoded><![CDATA[<p>I included jQuery by installing the <a href="http://ennerchi.com/projects/jrails">jRails</a> plugin, which seems nice because you can continue using rails&#8217; ajax helpers. But when I tried to implement a more special functionality by using the $.ajax function it proved impossible to set the Accept header for my request. Neither using the dataType option nor by setting it directly in a beforeSend function. The Accept header always read:<br />
<code><br />
text/javascript, text/html, application/xml, text/xml, */*<br />
</code><br />
I was about to uninstall and hate jQuery for the rest of my life.<br />
By coincidence I took a look at jrails.js which is a part of the jRails plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="javsscript" style="font-family:monospace;">(function($)
{
  $().ajaxSend(
    function(a,xhr,s){
      xhr.setRequestHeader(&quot;Accept&quot;,&quot;text/javascript, text/html, application/xml, text/xml, */*&quot;)
    }
  )
}
)(jQuery); 
[...]</pre></div></div>

<p>WTF? Is that documented anywhere?<br />
This hard coded <del>shit</del> stuff breaks rails&#8217; respond_to functionality, doesn&#8217;t it?t<br />
To be fair, it works if you use extensions to determine what datatype you expect. (Like /things/1.js)<br />
But I don&#8217;t do that when I build a custom ajax request where I can set the Accept header directly.</p>
<p><strong>Update:</strong><br />
Some research on the topic &#8220;accept header vs. extension&#8221; showed that in terms of cross browser compatibility I should favor the extension approach. And I&#8217;m not surprised it&#8217;s <a href="http://geminstallthat.wordpress.com/2008/05/14/ie6-accept-header-is-faulty/">Microsoft&#8217;s</a> <a href="http://intertwingly.net/blog/2007/08/23/Intermittent-IE-failures">fault</a> <img src='http://moserei.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/103/jquery-jrails-and-accept-header/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord: Overwrite attribute setter</title>
		<link>http://moserei.de/index.php/99/activerecord-overwrite-attribute-setter</link>
		<comments>http://moserei.de/index.php/99/activerecord-overwrite-attribute-setter#comments</comments>
		<pubDate>Wed, 17 Jun 2009 13:35:48 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[attribute setter]]></category>
		<category><![CDATA[overwrite]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=99</guid>
		<description><![CDATA[It&#8217;s somehow trivial, but I found out about &#8216;write_attribute&#8216; only just: [another case of rtfm]

class Thing &#60; ActiveRecord::Base
  def name=&#40;str&#41;
    str.upcase! unless str.nil?
    write_attribute&#40;:name, str&#41;
  end
end

Update:
Be careful if you use this with Datetime attributes.
]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s somehow trivial, but I found out about &#8216;<a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">write_attribute</a>&#8216; only just: [another case of rtfm]</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Thing <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> name=<span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span>
    str.<span style="color:#9900CC;">upcase</span>! <span style="color:#9966CC; font-weight:bold;">unless</span> str.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    write_attribute<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, str<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></pre></div></div>

<p><strong>Update</strong>:<br />
Be careful if you use this with <a href="http://moserei.de/index.php/113/activerecord-write_attribute-and-utc-conversion">Datetime attributes</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/99/activerecord-overwrite-attribute-setter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multi-line comments in ruby</title>
		<link>http://moserei.de/index.php/85/multi-line-comments-in-ruby</link>
		<comments>http://moserei.de/index.php/85/multi-line-comments-in-ruby#comments</comments>
		<pubDate>Tue, 07 Apr 2009 17:28:52 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby comment]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=85</guid>
		<description><![CDATA[I keep forgetting how to do them, so I post myself a little reminder here.
Just put

=begin
...
=end

around the stuff to be commented out. There must not be whitespaces before these keywords.
]]></description>
			<content:encoded><![CDATA[<p>I keep forgetting how to do them, so I post myself a little reminder here.</p>
<p>Just put</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#000080; font-style:italic;">=begin
...
=end</span></pre></div></div>

<p>around the stuff to be commented out. <strong>There must not be whitespaces before these keywords.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/85/multi-line-comments-in-ruby/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimistic Locking for Datamapper</title>
		<link>http://moserei.de/index.php/27/optimistic-locking-for-datamapper</link>
		<comments>http://moserei.de/index.php/27/optimistic-locking-for-datamapper#comments</comments>
		<pubDate>Wed, 01 Oct 2008 14:44:52 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[datamapper]]></category>
		<category><![CDATA[dm-optlock]]></category>
		<category><![CDATA[locking]]></category>
		<category><![CDATA[optimistic locking]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=27</guid>
		<description><![CDATA[Update(2009-3-3): dm-optlock now on github
Update(2009-1-10): New version
I should be learning for my test on next tuesday, but coding helps keeping me in a good mood.
Here is a little gem I will need in a future project for which I&#8217;ve choosen merb + datamapper. Datamapper doesn&#8217;t support any row level locking.
My code checks if the record [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update(2009-3-3):</strong> <a href="http://moserei.de/index.php/80/dm-optlock-now-on-github">dm-optlock now on github</a></p>
<p><strong>Update(2009-1-10): <a href="http://moserei.de/index.php/64/dm-optlock-v-014">New version</a></strong></p>
<p>I should be learning for my test on next tuesday, but coding helps keeping me in a good mood.<br />
Here is a little gem I will need in a future project for which I&#8217;ve choosen merb + datamapper. <a href="http://datamapper.org/">Datamapper</a> doesn&#8217;t support any row level locking.<br />
My code checks if the record you want to save was changed since you loaded it. If it was changed it raises an exception (DataMapper::StaleObjectError). It works quite like <a href="http://ar.rubyonrails.com/classes/ActiveRecord/Locking/Optimistic.html">ActiveRecord&#8217;s optimistic locking</a>.<br />
You need to define this column in your model you want to be locked:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">property <span style="color:#ff3333; font-weight:bold;">:lock_version</span>, <span style="color:#CC0066; font-weight:bold;">Integer</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span></pre></div></div>

<p>Anywhere in your code where you update your objects you should be prepared to handle the exception:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">begin</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@obj</span>.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:dummy <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">123</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;">#successful</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#008000; font-style:italic;">#validation errors?</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">DataMapper::StaleObjectError</span>
  <span style="color:#008000; font-style:italic;">#tell the user that he was too slow</span>
  <span style="color:#008000; font-style:italic;">#or even better: show him the conflicts and let him merge the changes</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The gem can be downloaded here:<br />
<a href="http://moserei.de/files/ruby/datamapper/dm-optlock-0.1.0.gem">dm-optlock-0.1.0.gem</a><br />
Please let me know what you think of it. I&#8217;ll try and put it on RubyForge, too.</p>
<p>(00:27) <strong>Little update:</strong><br />
I added a &#8217;set_locking_column&#8217; method.<br />
<a href="http://moserei.de/files/ruby/datamapper/dm-optlock-0.1.2.gem">dm-optlock-0.1.2.gem</a></p>
<p><del datetime="2009-03-03T19:13:31+00:00"><strong>Update:</strong><br />
<a href="http://rubyforge.org/projects/dm-optlock/">dm-optlock is on Rubyforge</a>.<br />
Now you can get it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> dm-optlock</pre></div></div>

<p></del></p>
]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/27/optimistic-locking-for-datamapper/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mergesort in Ruby</title>
		<link>http://moserei.de/index.php/20/mergesort-in-ruby</link>
		<comments>http://moserei.de/index.php/20/mergesort-in-ruby#comments</comments>
		<pubDate>Thu, 18 Sep 2008 12:22:24 +0000</pubDate>
		<dc:creator>Moser</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[mergesort]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://moserei.de/?p=20</guid>
		<description><![CDATA[While playing around with stable sorting I implemented Mergesort.

class Array
  def mergesort&#40;&#38;cmp&#41;
    if cmp == nil
      cmp = lambda &#123; &#124;a, b&#124; a &#60;=&#62; b &#125;
    end
    if size &#60;= 1
      self.dup
    [...]]]></description>
			<content:encoded><![CDATA[<p>While playing around with stable sorting I implemented <a href="http://en.wikipedia.org/wiki/Merge_sort">Mergesort</a>.</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> mergesort<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>cmp<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> cmp == <span style="color:#0000FF; font-weight:bold;">nil</span>
      cmp = <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>a, b<span style="color:#006600; font-weight:bold;">|</span> a <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> b <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> size <span style="color:#006600; font-weight:bold;">&lt;</span>= <span style="color:#006666;">1</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">dup</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
       halves = <span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>half<span style="color:#006600; font-weight:bold;">|</span>
        half.<span style="color:#9900CC;">mergesort</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>cmp<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
      merge<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>halves, <span style="color:#006600; font-weight:bold;">&amp;</span>cmp<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;
&nbsp;
  protected
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">split</span>
    n = <span style="color:#006600; font-weight:bold;">&#40;</span>length <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">floor</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
    <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>0..<span style="color:#9900CC;">n</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">+</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;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> merge<span style="color:#006600; font-weight:bold;">&#40;</span>first, second, <span style="color:#006600; font-weight:bold;">&amp;</span>predicate<span style="color:#006600; font-weight:bold;">&#41;</span>
    result = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">until</span> first.<span style="color:#9900CC;">empty</span>? <span style="color:#006600; font-weight:bold;">||</span> second.<span style="color:#9900CC;">empty</span>?
     <span style="color:#9966CC; font-weight:bold;">if</span> predicate.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>first.<span style="color:#9900CC;">first</span>, second.<span style="color:#9900CC;">first</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&lt;</span>= <span style="color:#006666;">0</span>
        result <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> first.<span style="color:#9900CC;">shift</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        result <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> second.<span style="color:#9900CC;">shift</span>
      <span style="color:#9966CC; font-weight:bold;">end</span> 
    <span style="color:#9966CC; font-weight:bold;">end</span>
    result.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span>first<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span>second<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></pre></div></div>

<p>It is not suitable for productive use as it&#8217;s quite slow. It&#8217;s about ten times slower than the stable_sort method from my <a href="http://moserei.de/?p=17">earlier post</a>.<br />
A little benchmark sorting an array of 2,000,000 random integers:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">                user     system      total        real
Original Sort <span style="color: #000000;">0.630000</span>   <span style="color: #000000;">0.010000</span>   <span style="color: #000000;">0.640000</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>  <span style="color: #000000;">0.639860</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
Stable Sort <span style="color: #000000;">12.780000</span>   <span style="color: #000000;">0.700000</span>  <span style="color: #000000;">13.480000</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">13.477982</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
Mergesort <span style="color: #000000;">109.170000</span>  <span style="color: #000000;">13.420000</span> <span style="color: #000000;">122.590000</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">122.617323</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://moserei.de/index.php/20/mergesort-in-ruby/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
