Dec 172009

I don’t really have a better name for this. It’s also not completely clean, but it works. I had, almost a year ago (362 days ago), written a blog post about lexicographic permutations. That was about permutations of elements within one array.
Someone on ruby-forum asked about permutations between multiple arrays. I found something in C#, which I was happy to transcribe to Ruby and tweak a little.

def array_permutations array, index=0
  # index is 0 by default : start at the beginning, more elegant.
  return array[-1] if index == array.size - 1 # Return last element if at end.
  result = []
  array[index].each do |element| # For each array
    array_permutations(array, index + 1).each do |x| # Permute permute permute
      result << "#{element}, #{x}"
  return result

So, we get this:

first = ['one', 'two']
second = ['three', 'four']
third = 'five', 'six']
result = array_permutations [first, second, third]
=> ["one, three, five", "one, three, six", "one, four, five", "one, four, six", "two, three, five", "two, thre
e, six", "two, four, five", "two, four, six"]


Edit – of course, my solution is hackish, and someone came up with a quicker and more elegant solution:

def fancy_array_permutation array
  return array[0] if array.size == 1
  first = array.shift
  return first.product( fancy_array_permutation(array) ).map {|x| x.flatten.join(" ")}

This gives the same result as above.

Dec 092009

I came across this beauty:

def same_modality? list
  check = list[0][0]
  list.each do |i|
    return false if check != i[0]
  return true

This was “necessary” because I got an array of one-element arrays back, and I wanted to check whether or not that one element was the same across the array.

Three seconds of thinking made me realize that just maybe, I could do this:

list.uniq.size == 1

I -like- Ruby.

Nov 062009

An auto-vivifying hash is a hash that lets you create sub-hashes automatically. This means that the following code becomes possible:

def cnh # silly name "create nested hash" {|h,k| h[k] =}
my_hash = cnh
my_hash[1][2][3] = 4
my_hash # => { 1 => { 2 => { 3 =>4 } } }

This is useful because it reduces the amount of logic in the code. No more “If sub-hash doesn’t exist, create it!”

Courtesy of Robert Klemme on



Facets has a way of doing an auto-vivifying hash, too! Thanks to Stackoverflow.

# Monkey patching Hash class:
# File lib/core/facets/hash/autonew.rb, line 19
  def self.autonew(*args)
    leet = lambda { |hsh, key| hsh[key] = new( &leet ) }

The standard new method for Hash accepts a block. This block is called in the event of trying to access a key in the Hash which does not exist. The block is passed the Hash itself and the key that was requested (the two parameters) and should return the value that should be returned for the requested key.

You will notice that the leet lambda does 2 things. It returns a new Hash with leet itself as the block for handling defaults. This is the behaviour which allows autonew to work for Hashes of arbitrary depth. It also assigns this new Hash to hsh[key] so that next time you request the same key you will get the existing Hash rather than a new one being created.

Sweet deal!

Oct 282009

I found a script that explains everything really well.
Credit goes to:
# CLOSURES IN RUBY Paul Cantrell
# Email: username “cantrell”, domain name “”
Along with this blog entry, it’s made the whole deal much easier to figure out.

Now.. Why didn’t I get that when I was looking at LISP?

Oct 122009

Someday I’ll come out with a proper revision numbering scheme for it.
I’ve implemented some Cucumber testing, the code now actually parses SGF files properly, including the KGS ones, it’ll work on Ruby 1.8.6, 1.8.7, 1.9.1, so .. It’s all good on that front.

The code’s not as clean as I’d like it to be, and probably not as commented, either.

I am not -saving- SGF files with it yet, but heck if I’m not parsing them. And now YOU can, too !

Sep 152009

So, I play weiqi (known as the game of go, but I prefer using the chinese name).
There are some SGF parsers out there for Ruby, but I wanted to write my own – the other ones didn’t seem comprehensive enough, didn’t seem Ruby-like enough in their objectifiying the women data.
My original goal was twofold: write a script to rename all the SGF files in my collection so they can be easily parsed later, and … Learn more Ruby!
Here is the github link:
It’s a work in progress. Right now it parses the SGF file properly, but the user has a fair amount of work to do to use the data.. Which, maybe, is the way it needs to be. But I’m gonna try to follow the principle of least surprise to the bitter end! :)

%d bloggers like this: