Part 3 ported to Ruby