Rewrite in ruby