first implementation from ppython to ruby