refactoring of ruby ifelse statement