translated demo from C to Ruby