refactored 3 in ruby