Wednesday, November 23, 2011

Exercise 26: Congratulations, Take A Test!: Learn Ruby the Hard Way: Practicum

So this marks the mid-way point for the book. 26 chapters, and I suppose, ideally, six months worth of programming. I'm going for broke and doing this in a much shorter time frame (partly because I have to, I need to be finished with Exercise 30 by the end of November. Remember when I first started talking about doing this? I made the ultimate in "bold boasts"; I tied my completion of this book to my annual performance review. What did we agree to? My completing up to Exercises 30 by the end of November and my completing the entire book by the end of January. Yes, it's actually spelled out exactly like that in my review goals... and you thought blogging about it was pressure (LOL!).

I have mixed emotions about posting these entries. In part, I feel like I might be "cheating" others by giving a blow by blow of all this; they may just use my answers and say "Oh, cool, that's how that works", and miss the whole point of going through all of these steps and actually learning the material. Still, my point with the Practicum series is to show the techniques, critique the exercises, and give my own take on these materials as though I were teaching them to someone else. Thus, I guess I will need to leave it up to the reader as to how they want to work with this, and if they want to actually do things "The Hard Way" or cheat and do what they think is an "easy" approach to learning this stuff. "Thems will be that thems will be" (or something like that). Also, I try my best to put the code and my changes into pictures, not into text that can be copied and pasted, so I guess if they want my answer, they will have to type it out anyway :).

So for this section, we are asked to work through a quiz. the quiz is to fix someone else's code and make it work. It's common to deal with other programmers' code. It's also common to deal with their insularity and their belief that their code is "just fine".

Exercises 24 and 25 have been cobbled together and errors have been introduced. Our job is to find the errors, fix the mistakes, and get the program to run correctly... without looking at the previous exercises to do it.

The "broken" file is here.

My "fixes" are here.


 What this shows us is that many of the errors we might put into our own code may seem blindingly obvious, but often they are subtle little misses, typos, or otherwise small things that can be infuriatingly difficult to find. There's still one area of the test I'm not sure I did right, and that has to do with the output of the "sorted sentence". I get that it's doing what it's supposed to do, but the repeated word and out of order with the pop is what has me scratching my head at the moment. If anyone else can tell me what I'm doing that's totally dumb, hey, I'll welcome the comments :).


Anonymous said...

Hello, I'm learning programming for the first time with learncodethehardway (and finding your blog very interesting as an accompaniment)... so I may be completely wrong, but I think your problem at the end is due to writing pop(1) instead of pop()

ary.pop → obj or nil ary.pop(n) → new_ary

Removes the last element from self and returns it, or nil if the array is empty.

If a number n is given, returns an array of the last n elements (or less) just like array.slice!(-n, n) does.

a = [ "a", "b", "c", "d" ]
a.pop #=> "d"
a.pop(2) #=> ["b", "c"]
a #=> ["a"]


Jonathan said...

Hi there!
I'm also a complete beginner in Ruby which I'm learning right now with Learn Ruby the Hard Way...
After going through the ex26 test, I wanted to see a correction and few comments on it to situate my learning compared to others.
Thanks for your blog, it is really nice to read!

If I may, I think I found a glitch in your correction for the
#Prints the first word after popping it off

According to rubydoc,
Array#pop() removes the last element from an array and return it. If a number n is given into the pop(n), it return an array with the n last numbers of the original array.

So I think using popping in the comment is part of a correction to do, giving a false hint on the solution;
According to an exercice before ex25.rb, I did the solution that way :

def puts_first_word(words)
word = words.shift()
puts word

Using shift(), is the exact same thing as pop(), but for the first element instead of the last;
meaning array.shift() returns the first element, and array.shift(n) returns the n first elements.

Hope this is right, and that I helped :)