rewrite in ruby