ruby version of problem 4