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.
This is impressive--keep it up!
ReplyDelete