Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Jared Armes
PLUS
Jared Armes
Courses Plus Student 6,391 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,888 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
Courses Plus Student 6,391 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,888 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
Courses Plus Student 6,391 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,888 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.