[for practice] Implement the algorithm in ruby