Sunday, April 22, 2012

Code Academy Week 2

Week two is officially in the books, and so far I am feeling pretty good.  We started off the week making sure everyone had a good understanding of arrays and hashes.  We also learned how to create a new Class and call methods inside those classes.  I felt confident that I knew exactly everything that was going on four hours into class on Tuesday.  Then the final hour of class came around, and I felt my head was going to explode.

Our TA, Raghu, gave us a challenge of calculating the same shopping cart exercise we were assigned as homework last week, only this time were were to convert it into a program that used classes and methods to make the calculations / print the total.  Usually we pair program, but on this day I was flying solo.  It felt like the entire class was marching forward, while I just sat there trying to think through where to even begin.  I started laying out my thoughts, and deleted them.  I then started again, and once again felt like I was heading down a dead end road.  After about 10 minutes, I asked for a little help on how to get started.  Come to find out, I was originally headed down the correct path.  Raghu reviewed the solution with the class very quickly.  I had no time to copy the solution down, but I felt like I understood the general thought process and how he came up with his solution.

After class, I tried to work on the same problem from scratch.  I was totally stumped, and could not get the program to return the shopping cart total value correctly.  I must have sat there for about an hour and half trying to figure it out before I decided to take a break and head home.  That night I took another stab at it.  I sat there for a couple more hours puzzled why my solution was not working correctly.  My code looked like:

  def total
    total = 0
    @cart_array.each do |i|
      total += i[:item].cost * i[:quantity]

Everything seemed to be correct.  I was pulling out the item, applying the cost to that item, pulling out the quantity from my array.  I then multiply them, and add that calculation to my running total.  I wasn't getting any errors, but I also was not getting the total cost for my shopping cart.  What could be wrong?  What I realized after staring at my screen for 3+ hours is that my method was not returning anything, because I had not told it to return anything.  Just because I was running the calculation did not mean that the method automatically is going to return that info.  Once I added the "return total" to my method, everything worked.

  def total
    total = 0
    @cart_array.each do |i|
      total += i[:item].cost * i[:quantity]
    return total

We also learned how to output data automatically into a html and css file.  It finally seemed like we were doing something real.  Something that I could easily apply to the app I want to build.  I spent that night and next day building a program that would add book titles, author, and reading level into a library.  The program then would output a summary of all books in that library.  This exercise helped me  make sure that I truly understood methods, and how they return information when called.

Thursday was exciting because we jumped into Rails for the first time.  We built a basic web app that had an index page with an ordered list of landmarks.  When clicking on the landmark, you would go to a details page that was dynamically put together by the ruby code embedded in the *.html file.  Once again, I went home and built a version of this same app that related to listing books in a library.  It had dynamic detail pages for each book that would list important information about the book that was clicked.  I really feel like all of this is coming together.

On Friday, I went to Eight Bit Studios to meet with my mentor Don Bora.  We went over the library app I had made the night before, and he introduced me to the "before_filter."  Basically this can be added in your controller file.  You can add a method that will then be used in all of your pages without repeating the code each time.  Don and I spent some time discussing my app, and where I should begin.  Basically I mapped out everything I want to build in the next 10 weeks.  I broke this up into the main sections such as user accounts, library, and backpack.  Under each of those sections I have a list of the the functions and sub-functions that will need to be built.  I am going to try and bite off a piece each week with what I learn in class.  I am really excited to get going on this.

Post a Comment