App Academy

Word Lengths Exercise

Perhaps it’s just me, but I found a couple things on this particular exercise that were confusing until I watched the Walkthrough.

Since Hashes are still a relatively new topic at this point it might be helpful if the hash created in the exercise was just named “hash” rather than “lengths,” especially since lengths is a function in the exercise already. I couldn’t quite wrap my head around the line:

words.each { |word| lengths[word] = word.length}

It felt more understandable to me when I refactored it to:

words.each do |word|
hash[word] = word.length
end

Also, I think when there’s a brand new topic, it might be easier to follow in long-hand vs. a one-liner like in the solution. While the one-liner syntax does carry over from the previous lessons, it’s still a bit easier to follow in the long-hand version.

Thanks.

The example they did is best. The point of writing code is to make it readable and effiecient. If there is a way to condense the code into something shorter, it is best practice to. Take that into consideration, the code you wrote is the same as the code they wrote, but the code they wrote is a 1 liner like you said. It might not make sense right now, but the further you go into the program you’ll start to see a number of shortcuts, that may not seem so understandable now, but the more you study them and start implementing them in your code the easier they start to become.

There’s gonna be a point in the course where you’re gonna have 200 lines of code and they are going to have only 50 lines, what you want to do is learn why and how they did it, since it’s gonna be best practice in the long run.

I’m gonna give you an example with a .map shortcut

#if you have an array: ["1","2","3"] and you want to change it to integers you can do 
def change_arr(arr)
arr.map! do |ele|
ele.to_i
end
end

I’m sure anyone can understand that, but if you were writing anything like that, the best way to write that is the following

def change_arr(arr)
arr.map!(&:to_i)
end