refactored the first ruby solution