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 trialJared Armes
Courses Plus Student 6,391 PointsCan'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.
def parse_answer ask(answer, kind="string")
answer = gets.chomp
answer = answer.to_i if kind == "number"
return answer
end
1 Answer
Tim Knight
28,888 PointsJared,
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
Courses Plus Student 6,391 PointsJared Armes
Courses Plus Student 6,391 PointsTim,
Thank you for your quick response! I have tried removing the entire line, as you have mentioned:
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
28,888 PointsTim Knight
28,888 PointsJarod I missed something in my first look at your question... note your method name, it should be:
You have an
ask
in there that doesn't need to be there.Jared Armes
Courses Plus Student 6,391 PointsJared Armes
Courses Plus Student 6,391 PointsThanks! That was, indeed, the problem. Though, I am still a little confused about when to use the "ask" method. Here is the corrected code:
Tim Knight
28,888 PointsTim Knight
28,888 PointsThe
ask
method was just another method that Jason created during video. In this case you're creatingparse_answer
. The latter is just being used in the quiz from what I can tell.