Stuck: Unit 4 - Sorting the Hash


This might sound confusing, but I've been stuck on Unit 4 for days, and I'm hoping someone can help me. I was able to advance from exercise 6 to 7, even though it shows me an error, but it let me continue.

First, here is my code:

puts "Your text here: "
text = gets.chomp

words = text.split
frequencies =

words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {word, count}

The error I see on the console is:

(ruby):7: syntax error, unexpected '}', expecting '='
(ruby):9: syntax error, unexpected $end, expecting '}'

But then at the bottom says "Way to Go!, Start New Lesson"

As a test, I used Codecademy code from Lesson 1 of this unit, called: "What you'll be building", and run it on the last exercise (Ex7):

puts "Text please: "
text = gets.chomp

words = text.split(" ")
frequencies =
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|a, b| b }

frequencies.each { |word, frequency| puts word + " " + frequency.to_s }

But even though it outputs it correctly, an alert message pops up and says "The program took too long to finish. Check your code for infinite loops and try again."

1 - Why is this happening?, Why does this code work on exercise 1, but if you use it at the end, it creates an infinite loop?

2 - There is a line that I can't seem to get past from the beginning of this unit:

frequencies = frequencies.sort_by {|a, b| b }

When I encounter it on exercise 6 says: "Use .sort_by to sort the frequencies hash by word count, like step 2 above. Store the result back in frequencies." So why would it work using letters a, and b {|a, b| b }, as in the code provided in lesson 1?, I used "{word, count}", and it gives me errors, why is this happening?

Hope someone can help!


Are you sure it complains about line 7 for that code?
When I run it locally I get:

dmontesi.rb:8: syntax error, unexpected '}', expecting '='
dmontesi.rb:9: syntax error, unexpected end-of-input, expecting '}'

If you compare the block you have at line 8 of your first code example with line 7 in the second code examples, can you see any differences? You can name things in there as you please, but you'll need to do the same things.

As for the infinite loop, I don't know what the last exercise is, maybe the code doesn't make sense in that context?


Thanks for the response ionatan!,

The syntax error I get seems to be with the lines below:

line 7: words.each { |word| frequencies[word] += 1 }
line 9: frequencies.reverse!

(ruby):7: syntax error, unexpected '}', expecting '='
(ruby):9: syntax error, unexpected $end, expecting '}'

Not sure why it would say expecting '=', and '}' ?, Any ideas?

Also, how come if I have the following line:
words.each { |word| frequencies[word] += 1 }

I can then add something like {|a, b| b }:
frequencies = frequencies.sort_by {|a, b| b }

what does the a, and b means in this case?, I'm just trying to wrap my head around this...



frequencies = frequencies.sort_by { |word, count| count }


That did it!, Thanks so much!