Port perl script to ruby