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.
Showing posts with label branching. Show all posts
Showing posts with label branching. Show all posts
Thursday, June 23, 2011
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.
Till then! Time to make dinner, and then back to go from learning to using.
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.
Subscribe to:
Posts (Atom)