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 trialDevin Scheu
66,191 PointsRuby Help
Question: Fill out the parse_answer method to return the answer passed in. If the kind is number, convert it to an integer using the to_i method before returning it.
Code:
def parse_answer(answer, kind="string")
if answer = kind
return answer
else
return answer.to_i
end
end
4 Answers
Sean T. Unwin
28,690 PointsYou can do this in one line code, aside from the return answer
statement.
The answer to the challenge is essentially given in the preceding video at around the 2min mark to 2min25sec, or in other words, the last 2 lines of the ask
method in the video. :)
Jessica H
16,612 PointsThat's a hard one just because it has to be written in a certain way.
def parse_answer(answer, kind="string")
answer = answer.to_i if kind == "number"
return answer
end
Anu Sandhu
2,719 PointsThanks Jessica!
Anand Mohan Duddella
Courses Plus Student 8,264 Pointstry without answer = gets.chomp and try with giving == (kind == "number")
Shaimoom Newaz
7,219 PointsCan someone explain why we remove the gets.chomp? Doesn't that code allow us to "return the answer passed in."
Anu Sandhu
2,719 PointsI have tried the code below, but am not passing the challenge. Any suggestions?
def parse_answer(answer, kind="string") answer = gets.chomp answer = answer.to_i if kind= "number" return answer end
viacheslavklepach
5,767 PointsTry without gets.chomp I was confused also)