Life is Perfectly Fair

It bugs me when people say “life isn’t fair.”

For one, it’s a disempowering belief. It encourages people to act as victims, or to view life as capricious. It’s much more productive to see yourself as empowered (at least to some extent).

For another, it can’t be true. If we take the definition of fair to be “that which is consistent with rules or logic,” and the definition of life to be “the physical and mental experiences that constitute existence,” then life has to be perfectly fair, by definition. It’s a matter of physics. The Universe operates with certain rules. You are a part of the Universe, and therefore bound by its rules. Now, you might say that it’s unfair that no one gave you all the rules, and even if they had, there’s no way you could possibly
understand all of them. However, working with incomplete information is one of the rules. It certainly makes the whole endeavor more difficult, but not unfair.

I think it would be better if people said “I didn’t get the outcome I wanted, and that’s because I don’t understand the rules yet.” Then, at least, you’re in the student’s mindset, and there’s the hope of improvement.

Making a Sinatra Web Application Work as a CGI

Here’s how I got a Sinatra version 1.0 web application to work via CGI using Rack version 1.1.0 behind an Apache httpd version 2.2.15 web server on Fedora 13 Linux. There are many ways to get a client request passed through to your application with httpd — it has a very flexible configuration — but the following is what I did, and should give you the general idea.

First, use the following command to check that httpd has the mod_rewrite module loaded:

$ apachectl -t -D DUMP_MODULES | grep rewrite_module
Syntax OK
  rewrite_module (shared)

Next, edit the relevant httpd configuration file (see the Apache documentation for your system), which for me was the /etc/httpd/conf/httpd.conf file, and add the following configuration items, where [path] is the absolute path to the directory of your Sinatra application, and [url] is the URL you want users to access your application with:

RewriteEngine On
RewriteRule ^[url].*$ [path]/dispatch.cgi [QSA,L]

<Directory [path]>
    AddHandler cgi-script .cgi
    Options +ExecCGI
</Directory>

You’ll see that the rewrite rule sends everything to a CGI file named dispatch.cgi along with any query strings. That’s the file that will kick start our application. So, let’s create that file and add the following lines to it:

#!/usr/local/bin/ruby

require "rubygems"
require "rack"
require File.join(File::dirname(__FILE__), "testapp.rb")

Rack::Handler::CGI.run(TestApp)

You can see that my ruby executable is in /usr/local/bin, which you can change as necessary. You can also see that I named my application’s main Sinatra::Base class TestApp, and that I named the file holding the TestApp class testapp.rb. Now, let’s create the testapp.rb file, and add the following lines:

require 'rubygems'
require 'sinatra'

class TestApp < Sinatra::Base
  get '' do
    "Hello from Sinatra!"
  end
end

Note that according to the Sinatra documentation, logging is “… disabled by default in Sinatra::Base subclasses” unlike when you run the application with something like ruby testapp.rb. In any event, you should be able to start creating your Sinatra application within TestApp now. Happy coding!

Book Review: Business Stripped Bare. Rating: 2/4.

Based on the little bit I know, I’m a fan of Richard Branson. However, his book “Business Stripped Bare” is fairly shallow. I didn’t find any of the deep business insights I was hoping for. He seems to rely on the same hackneyed material he himself chides, and spends enough time telling us how great Virgin is — as opposed to actually showing us the mechanics of how it got that way — that I felt sometimes like I was reading Virgin promotional material instead of a business book. I suggest you find your business lessons somewhere else.

Book Review: The Talent Code. Rating: 2/4.

I couldn’t finish “The Talent Code” by Daniel Coyle. Sorry. The writing isn’t too bad, but there’s just not enough rigorous thinking going on. The author frequently confuses correlation with causation, uses fictional stories to support scientific claims, makes dubious statistical assertions based on small sample sizes, and constantly cribs scientific research in a way that makes me wonder if they even understood what they were reading. The topic is interesting, but I recommend finding another book on the subject.

Making Ruby work with SELinux on Fedora

The version of Ruby that comes with Fedora 12 is:

$ yum info ruby
[...]
Version    : 1.8.6.383
[...]

I wanted to try some things with the latest version of Ruby which is 1.9.1-p376. So, I compiled it and installed it into /usr/local/ruby-1.9.1-p376 (which may not match the LFS — I’m not sure — but is my standard practice). However, since Fedora 12 ships with SELinux enabled, the Ruby shared libraries wouldn’t work (e.g. openssl.so). SELinux doesn’t like that they are relocatable shared libraries. To make SELinux happy, modified the persistent database of settings like this (Update: a “-a” flag may be necessary instead of a “-m” flag, depending on your situation):

$ sudo find /usr/local/ruby-1.9.1-p376/lib/ruby/1.9.1/i686-linux -name '*.so' -exec semanage fcontext -m -t textrel_shlib_t {} \;

…and then applied those settings to the files like this:

$ sudo find /usr/local/ruby-1.9.1-p376/lib/ruby/1.9.1/i686-linux -name '*.so' -exec restorecon -v {} \;

There may be a faster way of doing this, but what I did works:

$ ls -Z /usr/local/ruby-1.9.1-p376/lib/ruby/1.9.1/i686-linux/openssl.so
-rwxr-xr-x. root root system_u:object_r:textrel_shlib_t:s0 /usr/local/ruby-1.9.1-p376/lib/ruby/1.9.1/i686-linux/openssl.so

A Little Obsessed with Efficiency

I have a syndrome.

I’m constantly looking at how to simplify things or make them more efficient.
Things I’m actually able to make simpler or more efficient end up having a
Zen-like quality and that makes me happy. It’s kind of a drug. I call it a
syndrome, though, because I’m a little obsessed. I dwell on this stuff
even when it’s not worth it, or when the changes are out of my control.

A perfect example of this obsession came up recently in the restroom at work.
The restroom has two sinks next to each other, each usually having a bottle of
soap next to it on the side opposite the other sink. Yesterday, the left
bottle started going low, and would barely pump out any soap. I assumed the
janitors would fill it up, but they didn’t, and this morning at some point the
low bottle went empty. That instantly created a problem obvious to anyone
using the restroom: multiple people, two sinks, one bottle of soap sitting on
the far side of the right sink. People had to queue up to use the sink that had
soap, and that essentially made the other sink useless. Efficiency was cut in
half.

There’s an easy solution to this problem: move the full bottle to the middle. I
fixed the problem immediately after entering the restroom and seeing the
situation. What’s crazy to me is that by the time I got there, an entire work day
had gone by, and no one else had done anything. Didn’t they get caught in the
queue themselves? Couldn’t they feel the social friction? I was puzzled at why
no one would do anything.

Maybe the problem was overcoming inertia. Maybe they were all afraid of
overstepping their authority. Maybe they all thought it was somebody else’s
problem, or they were just being apathetic. I’m guessing this last reason is the
real reason. I wish the people around me were a little more engaged, because
a little more attention on everyone’s part could make life a lot more efficient. But
they’re not engaged.

I don’t know. Maybe that’s their syndrome.

Rails, Git and Empty Directories

It seems that Git ignores empty directories by default, just like Mercurial. Unfortunately, when you create a new Rails project, you also create empty directories. You can use this bash function (e.g. in your .bashrc file) to find those empty directories:

function find_empty_directories {
    if [ "$1" == "" ]; then
        start="."
    else
        start=$1
    fi

    find $start -type d | while read dir; do
        files=$(ls -a $dir | wc -l)

        # Adding up only the "dot" files should equal 2.
        if [ "$files" == "2" ]; then
            echo $dir
        fi
    done
}

This function could certainly be improved. For example, the output could be piped into another program or function that adds an empty file to each directory so that git doesn’t ignore it (e.g. I think the Rails team has started using .gitignore for this purpose). Alternatively, you could create a Rake task to do this.

Update: it pays to read the man page once in a while :) . This post gives a potentially simpler alternative, namely using find‘s -empty flag.