#!/usr/bin/env ruby # build double dabble in ruby so I can feel how it works number = 999 puts "Binary: #{number.to_s(2)}" binary_registers = number.to_s(2).split('').map(&:to_i) scratch_size = 4 * (binary_registers.count / 3).ceil scratch_space = Array.new(binary_registers.count + scratch_size) { 0 } binary_registers.each_with_index do |reg, i| scratch_space[-(binary_registers.count - i)] = reg end pp (scratch_space.count / 4).to_i binary_registers.count.times do (scratch_size / 4).to_i.times do |j| value = scratch_space[j * 4..j * 4 + 3].join.to_i(2) next unless value > 4 value += 3 scratch_space[j * 4..j * 4 + 3] = value.to_s(2).split('').map(&:to_i) end (scratch_space.count - 1).times do |j| scratch_space[j] = scratch_space[j + 1] end scratch_space[-1] = 0 pp scratch_space end