Saturday, June 25, 2011

Exceptions! And all that other nonsense.

I puttered through Eloquent JS chapters 4 and 5 after falling asleep last night about half way through chapter 4. Can I blame myself? The majority of the chapter was about cats. I get enough cats on reddit.

Today's valuable lesson, however, was in error management, something I completely suck at (some might say epicfail). I learned about catch and throw, which is useful when certain qualifiers come up, say... an error you expect, and you need the program to zoom through to another block of code, ignoring all previous code.

After reading about that, I said, hey! This is how I'm going to fix my secret santa program! As it turned out, it didn't work the way I wanted it to in Ruby, so I will have to practice catch/throw a bit more to make sure I get it. I tried 'raise' as well, but all that does is shoot out the error that I was already expecting. I imagine raise being more useful when you expect your code to produce an error later if certain parameters are not correct.

So after much strolling around the internet, I ran into my true solution (for now) on tutorialspoint.com, where I learned about something Chris Pine just glanced over when explaining loops. Apparently I can use 'retry' statements to start loops over if something goes wrong--in this case, if the person is left with himself to pick.

I'm going to try mu is too short's solution as well (listed here), because it just dawned on me how simple it would be in comparison.

Tomorrow is Engagement/Peace Love and Firefox at Dolores Park, so I think I'm gonna try and get down there for ice cream and swag.


EDIT: And before I forget, my Secret Santa code is on github here: https://github.com/podopie/startingruby/blob/master/secretsanta.rb Also celebrating +100 hits on the blog, which is awesome since I've only had it for two weeks! Here's hoping to getting at least 1000 hits before the summer ends.

No comments:

Post a Comment