Refactor ruby code