convert original ruby code to scala