ruby logic refactored