Adventures

Deeply Nested Hashes + JSON Parsing

Week 2 has finally passed! This week was pretty challenging. There was only one main focus throughout the whole week, and that focus was none other than the little devils called hashes. I have worked with hashes before, but it’s usually hashes that I make myself, not hashes that are given to me to modify. If only the problem stayed there, but it didn’t because these hashes were ridiculously long and ragged. There was really no set pattern I could’ve relied on, so I was forced to think of ways to implement these ideas in order to get the rspec tests passing in the most efficient and human-readable way. The times I found myself questioning myself the most was definitely when I had to modify deeply nested hashes. It was a real headache trying to figure out what exactly I am modifying at what given time. Luckily, I was introduced to the pry debugger and I was able to navigate into these states and figure out exactly what was happening. However, there were times where even if I knew what was happening and what all of my variables pointed to, I was still baffled by the weird return values. I had to step back and research all the little details about hashes and practice with large sets of data.

This is where JSON came in. I already had experience working with JSON because I was messing around with Firebase a while back. I was able to parse some sample JSON and work with it step by step in the debugger trying to observe the flow of the program. After I was very confident in my hash abilities, I was able to tackle Green Grocer, the hardest lab of the week. Green Grocer is a little application that creates a checkout system that calculates the total cost of a cart of items and apply discounts and coupons as necessary. Of course the purpose of this lab was to test our notion of simple data structures up ’till this point, so you can imagine that all the data was nested very deeply. I learned more about stubbing and how to use rspec, because there were a lot of tests that I had to pass there. This lab took up the majority of my time, mainly because the nested data structures were ragged and I had to test so many edge cases, before actually getting a chance to mess with the actual data I wanted. After I tested all of the edge cases, I was finally able to iterate through the list of items and build and simulate a supermarket environment where there’s a cart, the items can be on clearance, there are coupons and discounts that can be applied, and even the way that the teller checks the items and tallies the total.

Throughout the process of this lab, I learned a very valuable lesson, however. This lesson was something that caused me so many headaches that sometimes I just wished that I figured it out sooner: Arrays and hashes are always passed by reference when you pass them as parameters to methods. I can understand arrays, since in C++ it’s very similar, but hashes as well? And not only that, but when you are working with hashes and you try to clone them and create a copy of it so you can accidentally modify it since you’re actually creating a “shallow copy” of the hash, which will affect the original if nested deep enough. I had no idea this was possible. You can already imagine the trouble I went through trying to figure out why all of the data returned from my program was not accurate. I had to go step-by-step over every single line in my application, testing it against every possible test-case I could’ve possibly imagined, and of course all while questioning and doubting my own skills. If I can count the amount of times I wanted to give up, it would be impossible because I would run out of fingers. It’s not easy thinking that the code you have written is good, then having to re-check the logic just to find out it’s correct, but still having the wrong data spit out.

I still can’t believe all of this happened because I was working on the original data. This happened because when I created a copy, it wasn’t deep enough. I then had to research on how to create deep copies, and I found a solution! I was surprised to see that there were many people who had the same problem as me also.

 Marshal.load(Marshal.dump(data)) 

Using Marshal, I was able to create a deep copy and, then guess what? Miraculously all of my tests went from red to green and I was able to complete the lab! I was so frustrated yet happy at the same time. There was just a mixture of emotions I was feeling at the time because I spent so much of the little time I have going over my code thinking I probably messed up the logic somewhere, but in reality the logic was good and the problem had nothing to do with it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s