Wednesday, November 23, 2011
Exercise 26: Congratulations, Take A Test!: Learn Ruby the Hard Way: Practicum
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 :).
Subscribe to: Post Comments (Atom)
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"]
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 :
word = words.shift()
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 :)
Post a Comment