Create a Histogram Iterating Over the Hash


Hi all, can somebody help me please?

gets error in output:>
undefined local variable or method `c' for #<Context:0x10b31b8
what way does my code behave incorrectly?

puts "add text"
text = gets.chomp
words = text.split(" ")
frequencies =
words.each{ |word| frequencies[word] += 1}
frequencies[words] = frequencies.sort_by { |a, b| c } 
frequencies.each  { |word, frequency| 
    puts word + " " + frequency.to_s 


You're using a name that you haven't defined


you mean "frequency"? where should be defined please?


Check out the error message, it says which name isn't defined! Gotta read those things.

You might have a line number too, I can't tell if that error message is intact or if it's missing parts.

add text
-:6:in `block in <main>': undefined local variable or method `c' for main:Object (NameError)
        from -:6:in `each'
        from -:6:in `sort_by'
        from -:6:in `<main>'

That's a different ruby version, not sure how much of a difference that makes, but as you see there's a line number (aside from the name itself which is sufficient anyway)


right, found it, thanks a lot


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.