Ruby solution for problem 8