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.

Thursday, June 23, 2011

Method Magic!

After some playing around, I finally got the code to work. Here's the example from Eloquent JavaScript (finding out the *3 +5 equation to a number) written in Ruby.


The real interesting difference was in Ruby, I needed to call the 'goal' variable in both methods. I don't think I could have used an instance variable here, just given the way the code is written. Another simplicity with Ruby is that I don't need the return statement in order to call the variables. Other than that, it's fairly similar.

I also noticed that it was better to put the ' + 5' method before the ' * 3' method, only because the return tended to be shorter. The return the other way around for 24 would be (((((1 * 3) * 3) + 5) + 5) + 5), what a mess! It never gets the chance to say that 3 * 5 is the same as 5 + 5 + 5. How unfortunate.

I imagine it could also be completed using procs? Maybe I'll try that out tonight.

Probably will get one more Eloquent JavaScript chapter done tonight, and I'd like to recode the Secret Santa program if I'm still awake after that. Someone is coming by to purchase my television in the morning, so consider that an opportunity for productivity to increase.

Gordon shared this link with me today: Local variable's memory can be accessed outside its scope?! Also will get to this for reading tonight.

Aye I feel stupid after taking a day off. I'll go climb tomorrow for a bit in the morning, but tomorrow will be a serious reading/hacking day.

Moving on with Eloquent JavaScript

After cruising through Metal Gear Solid last night (such a great critical thinking game!), I read through chapter 3 of Eloquent JavaScript. I'm avoiding my original idea of skipping ahead after reading this chapter; there was some great content that helped explain a lot of the fundamental ideas in Ruby that I already was slightly aware of, but actually took the time to explain them in detail. These would be scope, stacking, and branching, the last of which was brand new to me.

Scope

Scope, to my understanding, is all the different levels of the program. I think the best personal example I can think of is a book. Books consist of the "book," or top-level, chapters, or different methods, and the literature (or just words). Variables can take place on various levels of your scope, so it's important to consider when writing a program the organization and scope of your variables. When I last posted on stackoverflow, I got asked why I used global variables ($), especially because Ruby makes it almost unnecessary. This would be... me not thinking about my scope.

Stacking

I posted my first question at stackoverflow on a stack error. Stacking essentially is Inception, or how many levels deep you go. Fortunately (or unfortunately, depending on how you look at it), computers only allow so many stacks, probably in the tens of thousands. I know you can increase it manually, but most of the time it's not necessary. Anyway, if you're using recursion, and the stacking NEVER STOPS, you'll eventually run into a stack error. They look a little different between Ruby:
SystemStackError: stack level too deep
and JavaScript:
RangeError: Maximum call stack size exceeded
but mean the same thing. Bright side of things: I can create a stackerror on command. The world is mine... recursively.

Branching

If you can figure out stacking pretty well, branching is the idea of working with a flow chart. Eloquent JavaScript adds to this mix or even more lucrative branching, letting more than branch work at the same time using ||, the computer equivalent of "or." I'll post an example of this later tonight in Ruby, borrowing the original example from Eloquent JavaScript just to practice my Ruby skills... or rather translation skills.

Till then! Time to make dinner, and then back to go from learning to using.

Wednesday, June 22, 2011

Started js, so much review

Jameson sent me over to Eloquent JavaScript, (http://eloquentjavascript.net/), and so far, a lot of it is review--to the point of kick in the face. I'll continue skimming along just for the ride, but I'm hoping to coast to where it starts becoming a bit more relevant. It's a really well put together website, but trying to do some of the code examples was a pain because the console wouldn't work correctly. Ah well.

Sent a few more applications in tonight to some startups. I'm really excited for this batch because I'm actually familiar with their products. I've used Meetup for... 2 years now, and Animoto was introduced to me when we had a TAPS presentation in the credential program at Davis.

Tomorrow's gameplan: next stage fixes to the Secret Santa program, and continuing along in the JavaScript book. Secret Santa has been widdled down from over 60 lines of code to now under 30, so I'm pretty proud of that. Just need to get one part working (the end of the loop) perfectly and I can then move this program onto the next stage.

Peace out, readers.

Tuesday, June 21, 2011

Secret Santa code done--for now

I finally got the code to work (it's on github).

So let me explain a few things of what it does. It's not the most efficient thing in the world (log linear), but it works.

1. There's a sorter in here that organizes the lists by last name. I don't remember why I put it in here, but once I can figure out how the code works without it, I'll remove it.
2. Each person gets randomly set to a gift person. As long as it's not a family person (as goes the Ruby Quiz rules), it deletes that person out of the gift array.
3. If the last person ends up with himself, the whole process is restarted.

Occasionally I get errors:

secretsanta.rb:51:in `+': can't convert nil into String (TypeError)

Not sure how to go about fixing that yet, so if I can't figure it out tomorrow, I'll send it over to stackoverflow. The error only comes up less than 1% of the time when using 100 random names, so it can't be too big of a deal.

Goodnight, rubyers! Hopefully I can go back and solve the rock paper scissors game tomorrow; if not, I'll find another rubyquiz hack to work on.

Sunday, June 19, 2011

errors errors errors.

I did about two hours of work so far today on my rock paper scissors game. I've defined a few rules, set the computer's initial select, and created a main menu that's used to process things. I also tried creating a global proc... but it looks like ruby doesn't like those semantics.

The idea was to save space. Why rewrite the same 3 lines of code every time the computer doesn't understand your input? So I came up with this:

didnotget = Proc.new do |goback|
puts 'I\'m sorry, I don\'t follow that command. Please try again.'
goback
end

This proc SHOULD come up any time the computer doesn't understand what's happening, and the variable at the end of the proc is used to recall the method the user was just on. This isn't working the way I wanted it to, because instead I get this error:
rpsgame.rb:91:in `initialize': undefined local variable or method `didnotget' for # (NameError)
from rpsgame.rb:122:in `new'
from rpsgame.rb:122

Ah well, I'll figure it out eventually. For now, I'm going to grab a taco from Casa at the Oxbow in Napa, and then get some quick grocery shopping done. I'll figure this out when I get home.