Rak, a grep replacement in pure-Ruby

Rak is a tool for searching directories for files matching a regexp, like a more convenient grep. It gives you pretty highlighted output, and uses the Ruby regexp syntax. To install type gem install rak.

Rak is implemented in pure Ruby, so it should work on all platforms. Windows folk will probably find they need to use the –nocolour option (I will fix this next time I boot into Windows for any reason).

NB. Rak is an almost perfect clone of the Perl tool Ack by Andy Lester. He should get all the credit for the idea.

Migrating from Subversion to git

You have a public svn repository served by svnserve on your server. You would like to migrate to a public git server. Here’s how.

Importing your svn repository

First we have to get your subversion repository into git. Let’s say your repository is in /home/dbl/svn/rak (this is totally made up - obviously). I would like to create a git repository in /home/dbl/git/rak.


$ cd /home/dbl/git
$ git-svnimport -v -C rak1 file:///home/dbl/svn/rak

This will initialize the git repository and import the svn history. The -v simply means I can watch the progress. -C rak1 is the directory the repository will be in - note that I put it into rak1 rather than rak. This is because we are now going to move it.

The git-svnimport command checks out a working copy into the “rak1″ directory. If you use the server as a working area and not just a server, that’s ok, but I just want to use it as a server so I have to get rid of the working copy. We do this by creating a ‘bare’ repository:


/home/dbl/git $ git clone --bare rak1 rak

If you look into rak/ now, you will see the contents of rak1/.git/. This is now a repository without a working copy, which is exactly what we want on a server.

Using git-daemon

You can remotely clone and use this repository right away with ssh, but I like to change my ssh port for security and that’s no good if you want your repository to be public. So let’s start git-daemon. The git-daemon listens on 9418, so you’ll need to make sure that is open. There are lots of options for this, but this works for me:


git-daemon --base-path=/home/dbl/git

You should probably add this to your cron to start on reboot. To get it working right away I actually typed: nohup git-daemon –base-path=/home/dbl/git &.

Now we have to tell the daemon which repos it’s ok to serve. git-daemon checks to see if the git-daemon-export-ok file exists, so:


touch /home/dbl/git/rak/git-daemon-export-ok

You can now clone your repository from your local machine with:


git clone git://www.yourserver.com/rak

The git-daemon doesn’t support push, so to push your changes back up to the server you’ll have to use ssh as in the git User Manual.

Setting up gitweb

If you would like a fancy repository browser like git.donttreadonme.co.uk, you could do worse than checking these instructions.

And that’s it!

Rubinius IRC Logs

Missing an issue of my favourite magazines - PC Plus, Edge and The Economist, causes me almost physical pain. I can’t stand the gap.

So I haven’t taken to IRC, just because I know I’m always missing something. This week I’ve been looking at the Rubinius project, which eschews a mailing list for IRC, and it seemed like a log of the discussions would be a good idea. So here’s a simple one in Ruby: the Rubinius IRC logs.

Rails Rumble

I’m browsing through the entries to the Rails Rumble and here are some that stand out:

Common elements:

Voting starts on Wednesday.

Matrioshka Hosting

For almost 5 years now, whenever I’ve learned a little bit more about the web, I’ve bought a new hosting package to support my new skills. And each time I’m kept the previous providers to avoid the hassle of moving things over. But it’s starting to look less and less convenient to have everything separated, and more expensive. Here’s my current situation:


Date   Host          Price  Space RAM   B/w
-------------------------------------------------
Jan 03 Yahoo         £4.50  2GB   n/a   100GB
Aug 05 1&1           £9.00  4GB   n/a   40GB
May 06 Dreamhost     £5.00  255GB n/a   3TB
Sep 06 Rails Machine £50.00 10GB  512MB 100GB
-------------------------------------------------
Totals               £68.50 271GB 512MB 3.2TB

The Reviews

Yahoo Geocities I set this up in January 2003 because I needed simple webhosting and wanted my own domain name. It’s tinkertoy but I am not aware of any downtime whatsoever in 4 and a half years.

1&1 Business Hosting Account Bought this so I could set up a wordpress blog. They have a very wide selection of services, though they appear to have written most of them themselves, which is odd. For instance, you can install the “1&1 Blog” software. Ummm, no thanks. Again, solid hosting with little or no downtime.

Dreamhost “Crazy Domain Insane” Shared Hosting Got this when I started learning about Rails in 2006. Truly extraordinary allocations marred by frequent network problems. Having said that, I haven’t noticed much downtime on my own server so perhaps that’s too harsh. Excellent web interface for installing packages. Setting up a new Subversion repository with websvn and access control is the work of a few moments. Very convenient.

Rails Machine VPS Got this when I wanted to deploy a couple of Rails apps. Comes pre-installed with a Rails-stack of Apache, MySQL and mongrel. Using the railsmachine gem to deploy is a well marked path, even for a noob like me. Little downtime, excellent support. A tad expensive perhaps, and only offers CentOS (enterprise Red Hat), when I would really have preferred a Debian derivative.

Yet another: Slicehost

All told I have 4 hosting or VPS accounts, which I bought over a period of 4 and a half years, costing me £70 every month. I don’t even want to do the calculations of how much this has cost in total. So I’ve been planning a consolidation for a while, and last month I discovered Slicehost.

There’s been a bit of buzz around them and they have quite a long waiting period. They predicted two weeks for me but it ended up about four, with little information along the way.

I don’t want to give the complete thumbs up yet, because I’ve only had the server online for a few days, but so far they seem like an excellent package:

The Plan

So the plan is to consolidate my current hosting into two Slicehost slices. One for my blogs, pages, email, mailing lists and subversion repositories. Then, once I’m confident that Slicehost know what they’re doing, a second for Says Who?.


Host             Price  Space RAM   B/w
-------------------------------------------------
Slicehost 256   £10.00  10GB  256MB 100GB
Slicehost 256   £10.00  10GB  256MB 100GB
-------------------------------------------------
Totals          £20.00  20GB  512MB 200GB

Total saving, £48.50 per month.

Printing Double Sided Without Exploding Your Brain

Here are some things that make my brain hurt:

Today I decided that I have chucked far too much wasted paper away, and wrote down the algorithm for printing double sided:


1.  If we are printing the first page go to 2, else go to 7.
2.  Print odd pages from entire range.
3.  If the last page is odd go to 4, even go to 5.
4.  Take all but the last printed sheet and place face down
    in printer tray, then go to 6.
5.  Take all printed sheets and place face down in
    printer tray.
6.  Print even pages from entire range, in reverse. STOP
7.  Print even pages from entire range.
8.  If the last page is even go to 9, odd go to 10.
9.  Take all but the last printed sheet and place face down
    in printer tray then go to 12.
10. Take all printed sheets and place face down in
    printer tray.
11. Print odd pages from entire range, in reverse. STOP

See… that’s not so bad.

Comparing Erlang and Prolog

In the back of my mind, I have for a little while known two things:

  1. Prolog is no language for a man to use.
  2. I should probably check out this Erlang thing.

So today I have a look at this getting-started-with-erlang tutorial, and lo! Perhaps my Prolog skills are not so useless after all.


% Erlang
-module(tut4).
-export([list_length/1]).

% simple function to return list length
list_length([]) ->
    0;
list_length([_F|R]) ->
    1 + list_length(R).


% Prolog
:- module(tut4).
:- export list_length/2.

% simple predicate to return list length
list_length([], 0).
list_length([_F|R], L):-
    L is 1 + list_length(R).

Similarities so far:

Differences:

Fix for Ruby Gnome GtkMozEmbed

After upgrading to Feisty, any attempt to use the Ruby-Gnome2 Gtkmozembed bindings fails with:


$ ruby ruby-gecko.rb
ruby-gecko.rb:203: [BUG] Segmentation fault
ruby 1.8.5 (2006-08-25) [i486-linux]

Aborted (core dumped)

After an hour of searching online, I discover that (a) the problem isn’t limited to Ruby, and (b) the fix is quite simple:


$ export LD_LIBRARY_PATH=/usr/lib/firefox

It’s things like this that make you crazy.

Null Objects

Tom Locke and I have similar proclivities. He says:


class Object
  def _?()
    self
  end
end

class NilClass
  def _?()
    SafeNil.new
  end
end

class SafeNil
  def method_missing(*args, &b)
    nil.send(*args, &b) rescue nil
  end
end

I say:


class Object
  def null
    n = nil
    def n.method_missing(*args)
      null
    end
    n
  end
end

The difference:


string_or_nil._?.length        # Tom's
(string_or_nil||null).length   # mine

I like being able to go more than one method deep,


(string_or_nil||null).length * 4

But that might not be to your taste.

Short is Good

Here’s a little Ruby snippet that I enjoy:


module Kernel
  alias fn lambda
end

Example usage:


lambda {a_call}.should raise_error
fn {a_call}.should raise_error

Courtesy of Arc.

← Previous Page

  • About

    Daniel Lucraft, London, UK. Interested in Ruby, Prolog and software.