Rewrite stack in ruby