Create a Histogram Iterating Over the Hash


#1


<>
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 = Hash.new(0)
words.each{ |word| frequencies[word] += 1}
frequencies[words] = frequencies.sort_by { |a, b| c } 
frequencies.reverse!
frequencies.each  { |word, frequency| 
    puts word + " " + frequency.to_s 
}


#2

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


#3

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


#4

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
wubwub
-: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)


#5

right, found it, thanks a lot


#6

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