this works, but checkout the answers - Ruby provides a simpler way to implement this functionality.