1

I want to ask a person how much they know about their friends and what their favorite game is. I want to print it after my loop iteration gathers the data. This is what I tried. I want the user to input their friends' name and their friends' favorite game and later print that, but I thought I did that with the two gets.chomp for friend and game:

friendgame_hash = {} #this is where the hash starts I believe
input = ""  #set input initially to nil
friend = "" #set friend to nil
game = ""   #set game to nil
# starts the input from the user to get the name of their friend and favorite game
puts "Enter name of friend, then their favorite game: or Press Enter to quit " #
input = gets.chomp
while input != "" do #continue getting name of friend and their favorite game 
 (puts "Enter name of friend: ")
 friend = gets.chomp
 puts "Enter friends favorite game: "
 game = gets.chomp

 friendgame_hash[friend] = game #if understanding correctly hash key=friend value=game
 input = gets.chomp #if user just presses enter they get out of while loop

end
puts "Here is the content that was in the hash: "
friendgame_hash.each do |key, value|
 puts "#{key} => #{value}"
end

But I get the following errors:

(eval):20: (eval):20: compile error (SyntaxError)
(eval):8: odd number list for Hash
(eval):9: syntax error, unexpected tIDENTIFIER, expecting '}'
    friend = gets.chomp
          ^
(eval):15: syntax error, unexpected '}', expecting kEND
(eval):18: syntax error, unexpected kDO_COND, expecting kEND
friendgame_hash.each do |key, value|
                       ^
(eval):18: syntax error, unexpected '|', expecting '='

I don't know where I am wrong. Any help would be outstanding. I am curious if I am on the right path or am I going about the problem wrong. Any assistance would be much appreciated.

3
  • When you use the do keyword after while you don't need the { and you should replace } with end Commented May 21, 2014 at 23:19
  • also the name of hash on the bottom is not consistent with the initialized hash use friendgame_hash for both Commented May 21, 2014 at 23:20
  • yeah I saw the error and fixed it thank you for pointing that mistake out I think my friend was asking me what movie they wanted to go see and I started typing that and didn't fix it sorry about that. Commented May 22, 2014 at 0:12

1 Answer 1

4

You have one variable name that looks like a typo, and also you don't use {} for while loops as in C. Here is a corrected version.

friendgame_hash = {}
input = ""
friend = ""
game = ""

puts "Enter name of friend, then their favorite game: or Press Enter to quit "
input = gets.chomp

# Curly braces after do were incorrect.
while input != "" do 
 puts "Enter name of friend: "
 friend = gets.chomp
 puts "Enter friends favorite game: "
 game = gets.chomp

 # There was a typo here
 friendgame_hash[friend] = game
 input = gets.chomp
end

puts "Here is the content that was in the hash: "
friendgame_hash.each do |key, value|
 puts "#{key} => #{value}"
end

For older versions of Ruby (1.8.7 and earlier), replace the last three lines with the curly brace syntax below.

friendgame_hash.each { |key, value|
    puts "#{key} => #{value}"
}
Sign up to request clarification or add additional context in comments.

7 Comments

am I going about this in the right fashion because I ran your fixed version and received the error shown below, and I edited the above version so thank you for pointing those out: error: eval):21: (eval):21: compile error (SyntaxError) (eval):21: syntax error, unexpected kDO_COND, expecting $end friendgame_hash.each do |key, value|
so my question is where am I wrong in the syntax that is producing the error because I honestly don't see it. Once again I am new to programming in general so I appreciate giving me the childs version of where I am going wrong.
I added comments to my original post to show where my thought process was. The place I don't understand fully is the friendgame_hash[friend] = game. I think I have the understanding listed above, but want to make certain so any comments on the overall thought process of this program and why I am getting the syntax error listed above would be greatly beneficial to my learning where I am going wrong with this program. Thank you once again for any pieces of wisdom as to what or how to make this better i.e. error free as it seems like it should be in my mind but obviously is not the case.
This code works fine for 1.9.3 and above versions of ruby. Yes, in few patches for ruby 1.8.7 it's throwing this error. If you will modify this code replacing do end block with a single line like this friendgame_hash.each{|k,v| puts "#{k} => #{v}"} you will no longer get that error.
@cloud9_925, Try cvibha's version if you are running an older version of Ruby.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.