refactaring ruby code