Iterating over the hash. What is wrong with this code!


#1

puts "game please"

text = gets.chomp

words = text.split("")

frequencies = Hash.new(0)
words.each do |words| frequencies[words] += 1
puts frequencies [words]
frequencies[words] = frequencies.sort_by {words, count}
end
frequencies[words].reverse!

frequencies.each do |word, frequency|
puts word + " " + frequency.to_s
end

This is what my code is and i keep having errors.

this is the error message

(ruby):10: syntax error, unexpected '}', expecting '='
(ruby):16: syntax error, unexpected $end, expecting '}'

if you could help me with this that would be great


#2

You've got the line number, check that line out and consider carefully what it is you want to be doing there and then look up how to do that something!


#3

I already tried that but, thanks


#4

How else would you fix it? Pretty sure that's the way to go.
Where did you get stuck? Above all, the point is that you need to try and see where you get stuck and figure out how to get around that obstacle.


#5

OK, thanks a lot for that info


#6

OK so it seems that I fixed that problem. The error message was giving me the wrong line number but, now its giving me a different error message without a line number and I don't Know what the error means.

here is my code

puts "game please"

text = gets.chomp

words = text.split("")

frequencies = Hash.new(0)
words.each do |words| frequencies[words] += 1
puts frequencies [words]
frequencies[words] = frequencies.sort_by [words, frequencies]
end
frequencies[words].reverse!

frequencies.each do |word, frequency|
puts word + " " + frequency.to_s
end

Here is the error message

wrong number of arguments(1 for 0)

If someone could tell me what that means that would be great


#7

I'm not very familiar with ruby but I believe it gave you the line number for where it could no longer make sense out of your code. Your mistake may have been earlier.

Here's what it looks like when I run your code:

$ vim hash.rb
$ ruby hash.rb 
game please
tic
1
hash.rb:11:in `sort_by': wrong number of arguments (1 for 0) (ArgumentError)
	from hash.rb:11:in `block in <main>'
	from hash.rb:9:in `each'
	from hash.rb:9:in `<main>

And that does provide a line number


#8

Thanks, that should help.


#9

Figured it out?
You gave it an argument where none was expected. Your argument was a list.
How is sort_by used, what needs to be given to it? Not an argument as the error points out, but something else


#10

I think I fixed that but, again it didn't work, here is the code.

puts "game please"

text = gets.chomp

words = text.split("")

frequencies = Hash.new(0)
words.each do |words| frequencies[words] += 1
puts frequencies [words]
end
frequencies[words] = frequencies.sort_by do |words, frequencies|
end
frequencies[words].reverse!

frequencies.each do |word, frequency|
puts word + " " + frequency.to_s
end

and the results page

game please
chess
1
1
1
1
2
c 1
h 1
e 1
s 2
can't convert String into Array


#11

Perhaps you'd want to run it through an interpreter with more verbose error messages.

In this case though, you can add puts's and see how far it executes before it crashes


#12

OK, I'll try that. Thanks


#13

Wait add puts where.


#14

In the code. To see how far it gets before it crashes.

Better to find/install another interpreter though, both because error messages are useful and because following error messages is a skill to practice.


#15

is there one you'd suggest


#16

well what I did was to pull up a terminal and type
sudo dnf install ruby
And a couple seconds later it was installed, linux distros are nice like that.

If I type
dnf info ruby
then I get some information about that package:

Name        : ruby
Arch        : x86_64
Epoch       : 0
Version     : 2.2.4
Release     : 47.fc23
Size        : 69 k
Repo        : @System
From repo   : updates
Summary     : An interpreter of object-oriented scripting language
URL         : http://ruby-lang.org/
License     : (Ruby or BSD) and Public Domain and MIT and CC0 and zlib and UCD
Description : Ruby is the interpreted scripting language for quick and easy
            : object-oriented programming.  It has many features to process text
            : files and to do system management tasks (as in Perl).  It is simple,
            : straight-forward, and extensible.

So what I'm using, and that seems to have pretty good error messages, is 2.2.4 from http://ruby-lang.org
I think the latest version is 2.3 though, if you're getting it from their website.

There are probably also online interpreters, you'll find those by googling "ruby online interpreter"


#17

OK, thanks a million


#18

OK, here is the new result window

jdoodle.rb:17:in block in <main>': no implicit conversion of String into Array (TypeError)
from jdoodle.rb:16:in
each'
from jdoodle.rb:16:in `'
game please
1
1
1
1
2
c 1
h 1
e 1
s 2

only problem, I don't understand what it means


#19

You've got a line number for where it happens, so one thing you can do is to look at that line and consider what your intention for that is, and then look up how to do that something.

"implicit conversion" means that the language is trying to convert your value to the type that's expected in that context. It's saying that the expected value type is an array, but you gave a string, and that it won't convert string to array implicitly (without being told to do so)


#20

But then how do I tell it to convert it to a array