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 trialMinca Daniel Andrei
6,245 PointsCode is OK but still won't pass me
I have 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.
What am I doing wrong, I've also tested it in my Console and it runs ok...
I can't seem to understand the requirement, it's a little vague, return what ?? The answer
or the kind
converted to_i
??
def parse_answer(answer, kind="string")
print "Your answer is: "
answer = gets.chomp
answer = answer.to_i if kind == "number"
return answer
end
puts parse_answer(32)
2 Answers
Justin Horner
Treehouse Guest TeacherHello Minca,
The challenge is assuming that the answer has already been provided from the console, it just needs to parse the answer for the given kind.
You can remove the puts and chomp lines and simplify the function to the following.
def parse_answer(answer, kind="string")
answer = answer.to_i if kind == "number"
answer
end
I hope this helps.
Travis Granger
2,745 PointsI don't understand why we don't need the "return" between lines 2 and 3 in @Justin's code.. would appreciate an explanation.
Thanks!
Justin Horner
Treehouse Guest TeacherHello Travis,
The reason the return is not needed is because Ruby supports implicit return. In Ruby, the last expression evaluated is used as the return value.
I hope this helps.
Minca Daniel Andrei
6,245 PointsMinca Daniel Andrei
6,245 PointsOh, I get it now! But please modify the requirement to say "assume that the
answer
has been passed already" it's a little tangled