Ruby Ruby Loops Build a Simple Contact List Methods That Return a Value

Jared Armes
Jared Armes
6,242 Points

Can't figure out how to return answer.

I have tried everything, but I just can't seem to figure this out. As you can see, I have nearly copied Jason's code from the previous video. My syntax is possibly incorrect; but I am having trouble understanding how the "ask" method works to begin with, and why we pass in those unique arguments that only resemble the contents of a Hash. If anyone can be kind enough to explain, I would greatly appreciate it.

ask.rb
def parse_answer ask(answer, kind="string")
  answer = gets.chomp
  answer = answer.to_i if kind == "number"
  return answer
end

1 Answer

Tim Knight
Tim Knight
28,852 Points

Jared,

You mostly have this correct. Since your getting answer as an attribute passed into the method you don't need to use gets.chomp (which you'd use if you were passing it as a parameter to the script). Just remove that line and you should be fine.

Jared Armes
Jared Armes
6,242 Points

Tim,

Thank you for your quick response! I have tried removing the entire line, as you have mentioned:

def parse_answer ask(answer, kind="string")
  answer = answer.to_i if kind == "number"
  return answer
end

But when I go to run the program, I am returned this error:

"SyntaxError: 4b9643bd-c33e-430e-8288-0db77ffcb229.rb:6: syntax error, unexpected '(', expecting ';' or '\n' def parse_answer ask(answer, kind="string") ^ 4b9643bd-c33e-430e-8288-0db77ffcb229.rb:6: syntax error, unexpected ')', expecting end-of-input"

Please note, that I also tried replacing "answer.to_i" with "gets.to_i" so that it asks the user for input, but this returned the same error.

Tim Knight
Tim Knight
28,852 Points

Jarod I missed something in my first look at your question... note your method name, it should be:

def parse_answer(answer, kind="string")
end

You have an ask in there that doesn't need to be there.

Jared Armes
Jared Armes
6,242 Points

Thanks! That was, indeed, the problem. Though, I am still a little confused about when to use the "ask" method. Here is the corrected code:

def parse_answer(answer, kind="string")
  answer = answer.to_i if kind == "number"
  return answer
end
Tim Knight
Tim Knight
28,852 Points

The ask method was just another method that Jason created during video. In this case you're creating parse_answer. The latter is just being used in the quiz from what I can tell.