<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.1" -->
<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/"
	>

<channel>
	<title>chazmeyers.com/blog</title>
	<link>http://www.chazmeyers.com/blog</link>
	<description>BLOGTASTIC!</description>
	<pubDate>Wed, 11 Jul 2007 20:07:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1</generator>
	<language>en</language>
			<item>
		<title>Double escaping backslashes in ruby.</title>
		<link>http://www.chazmeyers.com/blog/2007/07/double-escaping-backslashes-in-ruby/</link>
		<comments>http://www.chazmeyers.com/blog/2007/07/double-escaping-backslashes-in-ruby/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 20:07:24 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/07/double-escaping-backslashes-in-ruby/</guid>
		<description><![CDATA[If you have a string that looks like:
\
and you want it to look like:
\\
in ruby, you have to do the following:

Code (ruby)


&#160;


irb&#40;main&#41;:070:0&#62; str = &#34;\\&#34;


=&#62; &#34;\\&#34;


irb&#40;main&#41;:071:0&#62; puts str


\


=&#62; nil


irb&#40;main&#41;:073:0&#62; puts str.gsub&#40;&#34;\\&#34;, &#34;\\\\\\\\&#34;&#41;


\\


=&#62; nil


&#160;



In Ruby, you can use perl-style regular expression numbered group references in replacement strings (ie \\1 = the first captured group of the [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a string that looks like:<br />
\</p>
<p>and you want it to look like:<br />
\\</p>
<p>in ruby, you have to do the following:</p>
<div class="ch_code_container" style="font-family: monospace;height:300px;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">070</span>:<span style="color:#006666;">0</span>&gt; str = <span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">071</span>:<span style="color:#006666;">0</span>&gt; <span style="color:#CC0066; font-weight:bold;">puts</span> str</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">\</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">nil</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">073</span>:<span style="color:#006666;">0</span>&gt; <span style="color:#CC0066; font-weight:bold;">puts</span> str.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>&quot;</span>, <span style="color:#996600;">&quot;<span style="color:#000099;">\\</span><span style="color:#000099;">\\</span><span style="color:#000099;">\\</span><span style="color:#000099;">\\</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">\\</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">nil</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
<p>In Ruby, you can use perl-style regular expression numbered group references in replacement strings (ie \\1 = the first captured group of the regexp). So, to make a literal backslash in gsub, you need &#8220;\\\\&#8221;. And to make the second one, you need &#8220;\\\\\\\\&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/07/double-escaping-backslashes-in-ruby/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Slightly better YAMLization of Ruby Exceptions.</title>
		<link>http://www.chazmeyers.com/blog/2007/07/slightly-better-yamlization-of-ruby-exceptions/</link>
		<comments>http://www.chazmeyers.com/blog/2007/07/slightly-better-yamlization-of-ruby-exceptions/#comments</comments>
		<pubDate>Tue, 10 Jul 2007 18:27:18 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/07/slightly-better-yamlization-of-ruby-exceptions/</guid>
		<description><![CDATA[Consider the following irb session:

Code (ruby)


&#160;


irb&#40;main&#41;:001:0&#62; e = Exception.new&#40;&#34;Jerk!&#34;&#41;


=&#62; #&#60;Exception: Jerk!&#62;


irb&#40;main&#41;:002:0&#62; e.message


=&#62; &#34;Jerk!&#34;


irb&#40;main&#41;:003:0&#62; require &#8216;yaml&#8217;


=&#62; true


irb&#40;main&#41;:004:0&#62; YAML.load&#40;e.to_yaml&#41;.message


=&#62; &#34;Exception&#34;


&#160;



That sucks. Where did the &#8220;Jerk!&#8221; go? YAML ate it. If you include this code, you can keep your &#8220;Jerk!&#8221;:

Code (ruby)


&#160;


cpm@juno:~/helpticket-dev/trunk$ cat lib/better_exception_yaml.rb


require &#8216;yaml&#8217;


&#160;


class Exception


&#160; def Exception.yaml_new&#40; klass, tag, val &#41;


&#160; &#160; o = YAML.object_maker&#40; klass, &#123;&#125;&#41;


&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following irb session:</p>
<div class="ch_code_container" style="font-family: monospace;height:300px;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">001</span>:<span style="color:#006666;">0</span>&gt; e = Exception.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Jerk!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#008000; font-style:italic;">#&lt;Exception: Jerk!&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">002</span>:<span style="color:#006666;">0</span>&gt; e.<span style="color:#9900CC;">message</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#996600;">&quot;Jerk!&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">003</span>:<span style="color:#006666;">0</span>&gt; <span style="color:#CC0066; font-weight:bold;">require</span> &#8216;yaml&#8217;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">004</span>:<span style="color:#006666;">0</span>&gt; YAML.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span>e.<span style="color:#9900CC;">to_yaml</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">message</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#996600;">&quot;Exception&quot;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
<p>That sucks. Where did the &#8220;Jerk!&#8221; go? YAML ate it. If you include this code, you can keep your &#8220;Jerk!&#8221;:</p>
<div class="ch_code_container" style="font-family: monospace;height:300px;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cpm@juno:~/helpticket-dev/trunk$ cat lib/better_exception_yaml.<span style="color:#9900CC;">rb</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> &#8216;yaml&#8217;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> Exception</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> Exception.<span style="color:#9900CC;">yaml_new</span><span style="color:#006600; font-weight:bold;">&#40;</span> klass, tag, val <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; o = YAML.<span style="color:#9900CC;">object_maker</span><span style="color:#006600; font-weight:bold;">&#40;</span> klass, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; val.<span style="color:#9900CC;">each_pair</span> <span style="color:#9966CC; font-weight:bold;">do</span> |k,v|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; o.<span style="color:#9900CC;">instance_variable_set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@#{k}&quot;</span>, v<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; o.<span style="color:#9900CC;">exception</span><span style="color:#006600; font-weight:bold;">&#40;</span>val<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;message&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cpm@juno:~/helpticket-dev/trunk$ irb</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">001</span>:<span style="color:#006666;">0</span>&gt; <span style="color:#CC0066; font-weight:bold;">require</span> &#8216;lib/better_exception_yaml&#8217;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">002</span>:<span style="color:#006666;">0</span>&gt; YAML.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span>Exception.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;JERK&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_yaml</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">message</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#996600;">&quot;JERK&quot;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
<p>It still sucks because the backtrace is not preserved, but that&#8217;s because Exception#to_yaml doesn&#8217;t serialize that information. YAML was storing the message, but by default it stores it into a @mesg instance variable instead. Quite odd. </p>
<p>I wasn&#8217;t inclined enough to read up on the right way to yamlize new data.  Maybe later I&#8217;ll add backtracing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/07/slightly-better-yamlization-of-ruby-exceptions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Installing eventmachine on older FreeBSD&#8217;s.</title>
		<link>http://www.chazmeyers.com/blog/2007/07/installing-eventmachine-on-older-freebsds/</link>
		<comments>http://www.chazmeyers.com/blog/2007/07/installing-eventmachine-on-older-freebsds/#comments</comments>
		<pubDate>Thu, 05 Jul 2007 19:05:11 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/07/installing-eventmachine-on-older-freebsds/</guid>
		<description><![CDATA[I&#8217;m replacing some backgroundrb workers that do long, slow TCP connections with async eventmachine code in one of my Rails webapps. I ran into some problems installing eventmachine 0.7.2 on FreeBSD 4.8-STABLE with no root access, so I&#8217;m documenting how I got around these problems. I&#8217;m certain this isn&#8217;t the best way to fix this [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m replacing some <a href="http://backgroundrb.rubyforge.org/">backgroundrb</a> workers that do long, slow TCP connections with async <a href="http://rubyforge.org/projects/eventmachine">eventmachine code</a> in one of my Rails webapps. I ran into some problems installing eventmachine 0.7.2 on FreeBSD 4.8-STABLE with no root access, so I&#8217;m documenting how I got around these problems. I&#8217;m certain this isn&#8217;t the best way to fix this problem, but it worked for me.</p>
<p>First of all, &#8220;gem install eventmachine&#8221; fails because on FreeBSD, gcc expects &#8220;-pthread&#8221; when using the pthreads library. Linux gcc normally uses &#8220;-lpthread&#8221;. Instead, download and unpack the .tar.gz.</p>
<p>You&#8217;ll notice that &#8220;ruby setup.rb config&#8221; fails with the same error as &#8220;gem install eventmachine&#8221;. Open ext/extconf.rb in the editor of your choice and search for </p>
<div class="ch_code_container" style="font-family: monospace;height:100%;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">unless</span> have_library<span style="color:#006600; font-weight:bold;">&#40;</span>&#8216;pthread&#8217;<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#CC0066; font-weight:bold;">exit</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
<p>Comment this entire block of code out. It is dead to us. Now when you run &#8220;ruby setup.rb config&#8221;, a Makefile will be generated. Huzzah! Now, open ext/Makefile and look for a place to add -pthread. I added it to the LIBS environment variable. </p>
<p>Now, you may or may not be able to run &#8220;ruby setup.rb setup &#038;&#038; ruby setup.rb install&#8221; without problems.<br />
If so, great!<br />
If not, I have nothing else to share with you. Best of luck with your problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/07/installing-eventmachine-on-older-freebsds/feed/</wfw:commentRss>
		</item>
		<item>
		<title>My mind is gone.</title>
		<link>http://www.chazmeyers.com/blog/2007/04/my-mind-is-gone/</link>
		<comments>http://www.chazmeyers.com/blog/2007/04/my-mind-is-gone/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 20:59:41 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/04/my-mind-is-gone/</guid>
		<description><![CDATA[Today I decided to finally start teaching myself Python. Out of curiosity, I tried the following:

Code (python)


&#160;


&#62;&#62;&#62; class NoneType:


&#8230;&#160; &#160; &#160;def __str__&#40;self&#41;:


&#8230;&#160; &#160; &#160; &#160; &#160;return &#34;&#34;


&#8230;


&#62;&#62;&#62; None


&#62;&#62;&#62; str&#40;None&#41;


&#8216;None&#8217;


&#160;



Is it sick that I find these results disappointing? Has Ruby corrupted my mind? 
A few years ago I would have never even thought about overriding methods [...]]]></description>
			<content:encoded><![CDATA[<p>Today I decided to finally start <a href="http://www.diveintopython.org/">teaching myself Python</a>. Out of curiosity, I tried the following:</p>
<div class="ch_code_container" style="font-family: monospace;height:300px;">
<div style="">Code (python)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #ff7700;font-weight:bold;">class</span> NoneType:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&#8230;&nbsp; &nbsp; &nbsp;<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&#8230;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&#8230;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #008000;">None</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt;&gt; <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">&#8216;None&#8217;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
<p>Is it sick that I find these results disappointing? Has Ruby corrupted my mind? </p>
<p>A few years ago I would have never even thought about overriding methods of a language&#8217;s null type.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/04/my-mind-is-gone/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Double-you-tee-eff, Gmail?</title>
		<link>http://www.chazmeyers.com/blog/2007/03/double-you-tee-eff-gmail/</link>
		<comments>http://www.chazmeyers.com/blog/2007/03/double-you-tee-eff-gmail/#comments</comments>
		<pubDate>Wed, 21 Mar 2007 15:36:52 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/03/double-you-tee-eff-gmail/</guid>
		<description><![CDATA[I just noticed that if you rename or remove a label, filters don&#8217;t get updated appropriately. 
For example, let&#8217;s say that I don&#8217;t like having MySpace or FaceBook notifications in my inbox, so I create a MySpace label and and
FaceBook label and set up filters that archives mail and labels it appropriately.
Let&#8217;s skip ahead a [...]]]></description>
			<content:encoded><![CDATA[<p>I just noticed that if you rename or remove a label, filters don&#8217;t get updated appropriately. </p>
<p>For example, let&#8217;s say that I don&#8217;t like having MySpace or FaceBook notifications in my inbox, so I create a MySpace label and and<br />
FaceBook label and set up filters that archives mail and labels it appropriately.</p>
<p>Let&#8217;s skip ahead a few months. I now have filters and labels set up for MySpace, FaceBook, Virb, Friendster, LiveJournal and many other websites. I&#8217;m starting to experience label overload. To try to keep things under control, I rename the MySpace label &#8220;Social Networking Crap&#8221;.</p>
<p>This is what I would expect to happen:</p>
<ul>
<li>I should get some sort of warning that FaceBook, Virb, Friendster, LiveJournal, etc. labels are referenced in filters, so they shouldn&#8217;t be deleted.</li>
<li>Mail previously filtered under MySpace should notice the name change and now filter under &#8220;Social Networking Crap&#8221;.</li>
</ul>
<p>This is what does happen:</p>
<ul>
<li>FaceBook, Virb, Friendster, and LiveJournal mail just gets archived without any labeling. This is sub-optimal in my opinion, but somewhat understandable.</li>
<li>Mail previously filtered under MySpace is also archived without any labeling. WTF?</li>
</ul>
<p>Of course, the real solution in this hypothetical scenario is to not participate in as much social networking crap. But I digress.</p>
<p><strong>Update</strong> Did I speak too soon? It seems like sometimes the expected MySpace behavior works, while other times it doesn&#8217;t? Odd.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/03/double-you-tee-eff-gmail/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Broken static methods in PHP make me sad.</title>
		<link>http://www.chazmeyers.com/blog/2007/03/broken-static-methods-in-php-make-me-sad/</link>
		<comments>http://www.chazmeyers.com/blog/2007/03/broken-static-methods-in-php-make-me-sad/#comments</comments>
		<pubDate>Tue, 20 Mar 2007 18:19:00 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/03/broken-static-methods-in-php-make-me-sad/</guid>
		<description><![CDATA[A few weeks ago, I tried implementing ActiveRecord in PHP. What a fool I was. Consider the following:

Code (php)


&#160;


&#60;?php


&#160; class ActiveRecord &#123;


&#160; &#160; public static $table = &#34;NOTABLE&#34;;


&#160; &#160; static function selectAll&#40;&#41; &#123;


&#160; &#160; &#160; return &#34;SELECT * FROM &#34; . self::$table;


&#160; &#160; &#125;


&#160; &#125;


&#160;


&#160; class User extends ActiveRecord &#123;


&#160; &#160; public static $table = [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago, I tried implementing ActiveRecord in PHP. What a fool I was. Consider the following:</p>
<div class="ch_code_container" style="font-family: monospace;height:300px;">
<div style="">Code (php)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">class</span> ActiveRecord <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$table</span> = <span style="color: #ff0000;">&quot;NOTABLE&quot;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> selectAll<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;SELECT * FROM &quot;</span> . self::<span style="color: #0000ff;">$table</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #0000ff;">$table</span> = <span style="color: #ff0000;">&quot;Users&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
<p>Sadly, <tt>User::findAll()</tt> returns <tt>&#8220;SELECT * FROM NOTABLE&#8221;</tt>.  </p>
<p>I guess in order to implement ActiveRecord in PHP, you need to use some sort of combination of Singleton and Factory patterns? Gross. </p>
<p>Add this to the list of things that make me angry when I work in PHP. (The first item in that list is <tt>$a ||= &#8220;default&#8221;</tt> doesn&#8217;t work. Bah!)</p>
<p>It seems <a href="http://blog.joshuaeichorn.com/archives/2006/01/09/zactiverecord-cant-work/">I&#8217;m not the only person who was saddened by this</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/03/broken-static-methods-in-php-make-me-sad/feed/</wfw:commentRss>
		</item>
		<item>
		<title>6 &lt; 4.8?</title>
		<link>http://www.chazmeyers.com/blog/2007/02/6-48/</link>
		<comments>http://www.chazmeyers.com/blog/2007/02/6-48/#comments</comments>
		<pubDate>Wed, 28 Feb 2007 18:02:37 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[ruby]]></category>

		<category><![CDATA[General Programming Topics]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/02/6-48/</guid>
		<description><![CDATA[Like any good developer, I write code on a development server and then push the code to the production server. (Yay, SVN.) While testing out something, I noticed an odd inconsistency between the two.
Consider the following:

Code (ruby)


sock = TCPSocket.new&#40; ip, port &#41;



Fairly simple. When I run it on my test server, I got this result:

Code [...]]]></description>
			<content:encoded><![CDATA[<p>Like any good developer, I write code on a development server and then push the code to the production server. (Yay, SVN.) While testing out something, I noticed an odd inconsistency between the two.</p>
<p>Consider the following:</p>
<div class="ch_code_container" style="font-family: monospace;height:100%;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sock = TCPSocket.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> ip, port <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
</ol>
</div>
<p>Fairly simple. When I run it on my test server, I got this result:</p>
<div class="ch_code_container" style="font-family: monospace;height:100%;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Errno::EINVAL: Invalid argument - connect<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
</ol>
</div>
<p>When I see something like that, I presume that I made a blunder. Perhaps ip was an odd string, or port was a negative number. Who knows? I double checked myself. The ip and port were both valid.</p>
<p>Then I tried it out on our production server, just for the heck of it. I got this back:</p>
<div class="ch_code_container" style="font-family: monospace;height:100%;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Errno::ECONNREFUSED: Connection refused - connect<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
</ol>
</div>
<p>Turned out the server I was connecting to wasn&#8217;t up. A few minutes later, the server is back up and I am able to create sockets from both machines. What?</p>
<p>One possibility that crossed my mind was perhaps I was working with two different versions of Ruby. Check out the results from ruby -v:<br />
<code>dev: ruby 1.8.4 (2005-12-24) [i386-freebsd6]<br />
production: ruby 1.8.4 (2005-12-24) [i386-freebsd4.8]</code></p>
<p>The only difference is the version of freebsd. I can only guess that freebsd 6&#8217;s standard library gives less specific error messages than freebsd 4.8. Either that, or our host upgraded the standard library without bumping the freebsd version number. </p>
<p>How peculiar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/02/6-48/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Leaky Abstractions.</title>
		<link>http://www.chazmeyers.com/blog/2007/02/leaky-abstractions/</link>
		<comments>http://www.chazmeyers.com/blog/2007/02/leaky-abstractions/#comments</comments>
		<pubDate>Tue, 06 Feb 2007 16:36:34 +0000</pubDate>
		<dc:creator>chazmeyers</dc:creator>
		
		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[General Programming Topics]]></category>

		<guid isPermaLink="false">http://www.chazmeyers.com/blog/2007/02/leaky-abstractions/</guid>
		<description><![CDATA[You ever have a programming &#8220;Doh!&#8221; moment? I had one the other day. You could say I fell face first into a leaky abstraction. 
For those of you who are unfamiliar with the phrase &#8220;leaky abstraction&#8221;, Joel Spolsky coined it in his classic essay titled &#8220;The Law Of Leaky Abstractions&#8220;. Go read it. Just open [...]]]></description>
			<content:encoded><![CDATA[<p>You ever have a programming &#8220;Doh!&#8221; moment? I had one the other day. You could say I fell face first into a leaky abstraction. </p>
<p>For those of you who are unfamiliar with the phrase &#8220;leaky abstraction&#8221;, <a href="http://www.joelonsoftware.com/">Joel Spolsky</a> coined it in his classic essay titled &#8220;<a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">The Law Of Leaky Abstractions</a>&#8220;. Go read it. Just open a new tab. I&#8217;ll still be here.</p>
<p><hr /></p>
<p>This week at work, I was writing a prototype for an algorithm in Ruby. Once everything was working in memory, I had to see how well things would work when the algorithm iteratively stored it&#8217;s results into a database. I am a lazy man, so I just took my existing classes and made them into ActiveRecords. </p>
<p>Consider the following:</p>
<div class="ch_code_container" style="font-family: monospace;height:300px;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> Student &lt; ActiveRecord::Base</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> number_of_days_available_between<span style="color:#006600; font-weight:bold;">&#40;</span>start_date, end_date<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; start_date = start_date.<span style="color:#9900CC;">at_beginning_of_day</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; end_date = end_date.<span style="color:#9900CC;">to_end_of_day</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; count = <span style="color:#006600; font-weight:bold;">&#40;</span>start_date .. <span style="color:#9900CC;">end_date</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |sum, date|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; params = <span style="color:#006600; font-weight:bold;">&#123;</span> :start =&gt; date.<span style="color:#9900CC;">at_beginning_of_day</span>, :endd =&gt; date.<span style="color:#9900CC;">to_end_of_day</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; sum += <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">vacations</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> :first, :conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;start_date &gt;= :start AND end_date ,= :endd&quot;</span>, params <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; sum</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> probability_for_clinic</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; available_days = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">number_of_days_available_between</span><span style="color:#006600; font-weight:bold;">&#40;</span>start_date, end_date<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># if student has no days left, there&#8217;s no chance! return negative infinity</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006666;">-1</span>/<span style="color:#006666;">0.0</span> <span style="color:#9966CC; font-weight:bold;">if</span> available_days == <span style="color:#006666;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assignment_count = Assignment.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span>:conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;student_id = :me AND clinic_detail_id = :clinic&quot;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:me =&gt; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">id</span>, :clinic =&gt; clinic.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; clinic_days_left = <span style="color:#006600; font-weight:bold;">&#40;</span> clinic.<span style="color:#9900CC;">duty_length</span> - assignment_count <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_f</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; clinic_days_left / available_days</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">students = Student.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>:all<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">student_probabilities = students.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |hsh, student|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; hsh<span style="color:#006600; font-weight:bold;">&#91;</span>student<span style="color:#006600; font-weight:bold;">&#93;</span> = student.<span style="color:#9900CC;">probability_for_clinic</span><span style="color:#006600; font-weight:bold;">&#40;</span> clinic, start_date, end_date<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; hsh</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">students = students.<span style="color:#9900CC;">sort</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|s1, s2| student_probabilities<span style="color:#006600; font-weight:bold;">&#91;</span>s2<span style="color:#006600; font-weight:bold;">&#93;</span> &lt;=&gt; student_probabilities<span style="color:#006600; font-weight:bold;">&#91;</span>s1<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
</ol>
</div>
<p>I started it up and waited for what felt like an eternity. Much frustration and a ^C later, I realized I went wrong somewhere. </p>
<p>That&#8217;s when I remembered that ActiveRecord could be asked to <a href="http://weblog.jamisbuck.org/2007/1/31/more-on-watching-activerecord">write everything it was doing to STDOUT</a>. Boy was I shocked! </p>
<p>I restarted the program to be greeted by a sea of &#8220;SELECT * FROM Vacactions&#8221; statements. As previously written, my script was generating about <a href="http://www.google.com/search?q=90%21">90!</a> selects to the vacations table for each student. Even with just over 1000 students, this took a silly ridiculous amount of time. My poor little script was in dire need of caching.</p>
<p>My new version isn&#8217;t as pretty to read and it could still use a bit more work, but it is much quicker:</p>
<div class="ch_code_container" style="font-family: monospace;height:300px;">
<div style="">Code (ruby)</div>
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sql = <span style="color:#996600;">&quot;clinic_detail_id = :clinic AND student_id IN (:students) AND day &gt;= :start_date AND day &lt;= :end_date&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">args = <span style="color:#006600; font-weight:bold;">&#123;</span> :students =&gt; students, :clinic =&gt; clinic.<span style="color:#9900CC;">id</span>, :start_date =&gt; start_date, :end_date =&gt; end_date <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cached_assignments = Assignment.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> :all, :conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span> sql, args <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sql = <span style="color:#996600;">&quot;student_id IN (:students) AND start_date &gt;= :start_date AND end_date &lt;= :end_date&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cached_vacations = Vacation.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> :all, :conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span> sql, args <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; students = Student.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>:all<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; student_probabilities = students.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |hsh, student|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#008000; font-style:italic;"># find the number of days student will be at school in semester.</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; available_days = <span style="color:#006600; font-weight:bold;">&#40;</span>start_date .. <span style="color:#9900CC;">end_date</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |sum, date|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; sum += <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">unless</span> cached_vacations.<span style="color:#9900CC;">detect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |v| v.<span style="color:#9900CC;">student_id</span> == student.<span style="color:#9900CC;">id</span> &amp;&amp; <span style="color:#006600; font-weight:bold;">&#40;</span> v.<span style="color:#9900CC;">start_date</span> .. <span style="color:#9900CC;">v</span>.<span style="color:#9900CC;">end_date</span> <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span> date <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#008000; font-style:italic;"># if there are no days left, there is no chance.</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> available_days == <span style="color:#006666;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; hsh<span style="color:#006600; font-weight:bold;">&#91;</span>student<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">-1.0</span>/<span style="color:#006666;">0.0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assignments_served = cached_assignments.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |a| a.<span style="color:#9900CC;">student_id</span> == student.<span style="color:#9900CC;">id</span> &amp;&amp; <span style="color:#006600; font-weight:bold;">&#40;</span> start_date .. <span style="color:#9900CC;">end_date</span> <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span> a.<span style="color:#9900CC;">day</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">size</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; hsh<span style="color:#006600; font-weight:bold;">&#91;</span>student<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#40;</span> clinic.<span style="color:#9900CC;">duty_length</span> - assignments_served <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_f</span> / available_days</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; hsh</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">s = students.<span style="color:#9900CC;">sort</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |s1, s2| student_probabilities<span style="color:#006600; font-weight:bold;">&#91;</span>s2<span style="color:#006600; font-weight:bold;">&#93;</span> &lt;=&gt; student_probabilities<span style="color:#006600; font-weight:bold;">&#91;</span>s1<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
</ol>
</div>
<p>Do note that if you squint hard enough, these two algorithms look about the same in terms of &#8220;big O&#8221;. </p>
<p>Sometimes the devil really is in the details.  The devil in this case was hiding a 90! constant under an abstraction. Jerk!</p>
<p>Abstractions lie. Be prepared to deal with it.<br />
Or, in the oft spoken words of Tim Gunn, &#8220;Make it work.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chazmeyers.com/blog/2007/02/leaky-abstractions/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
