Day 33: Making the Decision to Write My First App

Well, I have made a decision. I will be writing my first app over the coming days (possibly weeks).

I have decided to make an app in Ruby to track books I have read or listened to. I would like to eventually be able to assign a short, one sentence review to each. I would also like to figure out a way to mark the books as read, listened to, or wish list. Let’s get started.

Preface: I will be borrowing code from previous exercises. I will make a lot of mistakes. I will not apologize for either. I will say thanks to the previous exercises, however.

OK, here goes. The first bit of my code adds a menu that barely works and allows me to add a book.

OK, this is what happens:

demosthenes131@book_tracker:~/workspace $ ruby book_tracker.rb
Select an option from the below choices
a: Add a Book
v: View the Books
s: Search the Books
e: Exit
a
Add a book!
Ender’s Game
Select an option from the below choices
a: Add a Book
v: View the Books
s: Search the Books
e: Exit
a
Add a book!
Old Yeller
Select an option from the below choices
a: Add a Book
v: View the Books
s: Search the Books
e: Exit

Weird. It won’t keep the previously entered book. I need to create an array to collect these, I’m guessing…

And I have banged my head for an hour on this. I scrapped the whole thing and went simple:

and the results:

demosthenes131@book_tracker:~/workspace $ ruby book_tracker.rb
Add a book!
Ender
[“Ender”]
Add a book!
Shadow
[“Ender”, “Shadow”]
Add a book!
Hitchhiker
[“Ender”, “Shadow”, “Hitchhiker”]
Add a book!
Dresden
[“Ender”, “Shadow”, “Hitchhiker”, “Dresden”]
Add a book!

OK, this works. Let’s go a little more fancy. I want it to loop until I end it, and allow me to enter as many books as I decide. When I end it, I want it to print the list of books and then break. I worked it out a bit:

book_list = []

and it works! I was as surprised as you are:

demosthenes131@book_tracker:~/workspace $ ruby test.rb
Do you want to add a book? (yes or no)
yes
Add a book!
Ender’s Game
Do you want to add a book? (yes or no)
yes
Add a book!
Dresden Files
Do you want to add a book? (yes or no)
yes
Add a book!
Hitchhiker’s Guide to the Galaxy
Do you want to add a book? (yes or no)
no
[“Ender’s Game”, “Dresden Files”, “Hitchhiker’s Guide to the Galaxy”]

I also want to be able to add a one sentence review. I think a hash would work for this. Here is my attempt:

And the results:

demosthenes131@book_tracker:~/workspace $ ruby test.rb
Do you want to add a book? (yes or no)
yes
Add a book!
Ender
How about a short, one sentence review? (yes or no)
yes
Add your review!
Great book!
Do you want to add a book? (yes or no)
yes
Add a book!
Dresden
How about a short, one sentence review? (yes or no)
yes
Add your review!
Lots of fun
Do you want to add a book? (yes or no)
no
[“Ender”, “Dresden”]
{“book”=>[[“Ender”, “Dresden”]], “review”=>[[“Great book!”, “Lots of fun”]]}

I tried to work out the way to display this right. No luck as of yet. My last update:

and the result:

demosthenes131@book_tracker:~/workspace $ ruby test.rb
Do you want to add a book? (yes or no)
yes
Add a book!
Ender
How about a short, one sentence review? (yes or no)
yes
Add your review!
Awesome book
Do you want to add a book? (yes or no)
yes
Add a book!
Dresden
How about a short, one sentence review? (yes or no)
yes
Add your review!
urban fantasy fun!
Do you want to add a book? (yes or no)
no
[“Ender”, “Dresden”]
{“book”=>[[“Ender”, “Dresden”]], “review”=>[[“Awesome book”, “urban fantasy fun!”]]}
book – [[“Ender”, “Dresden”]]
review – [[“Awesome book”, “urban fantasy fun!”]]