Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Hi there im having problems with this syntax of ruby. Was hoping if anyone can help me.

1.puts"Text here:"

  1. text = gets.chomp
  2. words = text.split
  3. frequencies = Hash.new(0)
  4. words.each { |words| frequencies[words] +=1 }

  5. frequencies = frequencies.sort_by { |k, v| v }

  6. frequencies.reverse!

  7. frequencies.each do |words, frequencies|

    1. puts words + " " + frequencies.to_s 10.end

I didn't type this myself. I get it's purpose however there is certaint things in the syntax that i don't understand. There's a number of small questions i have about this. For one line #4 does the has have to be set to 0? Isn't it already empty since its a new hash?

Question 2 on line # 5 why is frequencies not factored in a variable with words also what does frequencies[words] do exactly? Is it making two variable relevant to eachother?

Question 3 on line # 6 I get the { |k,v| } but whats with the extra v?

question 4 on line # 9 WORDS + " " + FREQUENCIES.to_s What is this exactly doing having trouble seeing what the " " are for.

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Answer 0: Stylistically the names of the block arguments should be singular, not plural. Because the block arguments are not the same as the variables outside the block, I think it's confusing to name them the same thing:

puts"Text here:"
 text = gets.chomp
 words = text.split
 frequencies = Hash.new(0)
 words.each { |word| frequencies[word] +=1 }

 frequencies = frequencies.sort_by { |k, v| v }
 frequencies.reverse!
 frequencies.each do |word, frequency|
     puts word + " " + frequency.to_s
end

Answer 2: frequencies is a hash of each unique word in the text and the number of times that word occurs in the text. So every time you encounter a word, you increment it's count by one.

Answer 1: No value isn't the same as a value of 0. Hash.new(0) sets the default value for keys (ie. words) to 0. Hash.new() would set the default value to nil, which would cause an error when you tried to add one to it.

Answer 3: | k,v | sends in two block arguments and the v returns just the value. (ie., blocks work like nameless functions with arguments and return values.

Answer 4: The puts at the end prints each word with it's frequency. The + " " + just adds a space between them.

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Things make a bit of sense now thank you so much. If i may ask though. When frequencies [word] was made what does the word arguement do to the frequencies. Like when do i know to include arguement's in these methods?