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 trialSandra Grassl
6,933 PointsMethods that return a value Code Challenge/Ruby Loops
In that code challenge I get asked to
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.
This is the given code:
def parse_answer(question, kind="string") end
As this is very similar to the previous video I thought I input: def parse_answer(question, kind="string") answer = gets.chomp answer = answer.to_i if kind == "number" return answer end
But I don't pass the challenge with this. Can anybody explain to me what this challenge is expecting?
Cheers
4 Answers
Andrew Stelmach
12,583 PointsThe question isn't written that well, actually, because it should say 'if kind is "number"', not 'if kind is number'.
'answer' is an argument and so is 'kind' (set to "number" by default).
def parse_answer(answer, kind="string")
if kind == "number"
answer.to_i
else
answer
end
end
If you don't fully understand what's going on here, post it up and me or someone else will help you out.
Sandra Grassl
6,933 PointsThank you. Now I agree, that this task is not written that clear.
BEAU WITKA
20,594 PointsI agree as well, this was the first Treehouse exercise I was stumped on. Judging from what they just taught us in the previous lesson, I would expect the code to look more along the lines of:
def parse_answer(answer, kind="string") puts "Enter something, anything: " answer = gets.chomp answer = gets.chomp.to_i if kind == "number" return answer end
cbob
3,233 Pointsthanks!
Jitli Gan
2,668 PointsJitli Gan
2,668 PointsWhy didn't you return the value for answer?
Oğulcan Girginc
24,848 PointsOğulcan Girginc
24,848 PointsJitli Gan
You don't need to write
return
. It will automatically returnanswer
anyway.