rewrite ruby from perl