PHP code to Ruby