# 8/8 You did it!

#1

I would just like to walk through my code, line by line to explain my understanding/explain also where it gets kind of unclear for me. Although it works, I want to solidify by explaining and then have others correct me or explain parts where I am getting confused. So here goes.

puts "Please enter some text: "
text = gets.chomp
1. Get our text input from the user. Use gets to grab input... set that string to a variable called text

words = text.split(" ")

1. Create an array. The array called "words" is created by splitting the string at every point where we find a space. So "Hello there it's me" becomes ["Hello" "there" "it's "me"]

frequencies = Hash.new(0)

1. create a new hash called "frequencies" and set it's default value to 0.

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

1. Iterate over the words array using each. For each word found, add a value to frequencies and increase it's value by one.

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

1. Sort the frequencies hash using sort_by method. Our key is the word, the value we want to sort by is count. Grab the count value from the hash. Store that new value in frequencies.

frequencies.reverse!

Using the count values from earlier, reverse the numerical order of the count values so the larger numbers appear first.

frequencies.each {|word, count| puts "#{word} #{count}"}

Iterate over the hash. Select the key and value pairs from the hash. Put them on the screen.

#2

`gets` is for input
`.chomp` is to remove the newline (LF) character from the end of the input string.

Remember, array data is comma separated.

That would be plural, `values`, as in the value associated with each new key initially. This allows us to accumulate on it. 0, 1, 2, ...

We're not actually sorting the hash, but the data and assigning the array back to `frequencies`, which goes from being a hash to being an array of tuples.

The ! tells Ruby to not make a copy, but to reverse it in place.

Iterate over the array, as indicated above.