This morning I was working out my newbie hash-fu.
After awhile I was able to wrap things up by working on this project I recently completed at Code Academy.
I’ll call it movie_manager. Movie Manager allows a user to enter movie titles, and add a rating to each.
Lets see what it looks like.
First up is creating a hash to store in the variable movie_list. This is where all data for the program will be utilized.
The user is offered four options which are stored in the ‘choice’ variable. Then a case statement is created based off of the four options the user has been offered in the program, the first case being ‘add’.
Note that the title variable was converted from a string into a symbol. The ‘title.to_sym’ not only ensures that all titles will have the same data-type, using symbols is also a more efficient way of adding and retrieving strings. In this ‘add’ case it is checking to see if the movie title is unique. If there isn’t already a movie in the user’s current list that matches the title, the user is prompted to offer their rating for the new movie. If movie title happens to pre-exist in the movie_list hash,than the movie title and rating will be shown, along with a message to the user that the movie is already a part of the list.
The next case the user has the option of selecting is ‘update’.
When the user selects this case, the program goes through ‘movie_list’ until it finds the associated title, and then allows the user to re-asses their feelings by updating their previous rating. If the program finds that the particular entry isn’t in the list, then a message is sent to the user stating that there is no movie by that title in the hash.
In the event that our user decides to select ‘display’, then the program prints each movie title and rating on its own line, making for a decent visual format that should be aesthetic enough for the client to consider readable.
When ‘delete’ is being utilized, the program first checks that the movie exists. If it doesn’t exist then the appropriate message broadcasts to the client. On the other hand, if the users title does match with a pre-existing entry within ‘movie_list’, then the program converts the users’ info into a symbol and deletes it from ‘movie_list’.
And there we go!
A simple, yet interactive movie and ratings manager in Ruby.