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 trialrdaniels
27,258 PointsMethods that Return a Value in Ruby Track
I'm doing the Code Challenge in the "Methods that Return a Value". The first portion of the code is given. The instruction are: 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.
My attempt is:
def parse_answer(answer, kind="string") answer = gets.chomp answer = answer.to_i if kind=="number" return answer end
Not sure what/why it is not working... any help will be appreciated!
1 Answer
Michael Hulet
47,913 PointsI think you have the right idea, but you're getting your input from the wrong place. In your method, you're calling gets.chomp
and operating on that. However, this is not what the question is asking. The question asks you to operate on the answer
variable that is passed to the method, which you can work on without assigning it first. In other words, all you need to do is delete the first line of your method, and I believe it will work, like this:
def parse_answer(answer, kind="string")
answer = answer.to_i if kind=="number"
return answer
end
rdaniels
27,258 Pointsrdaniels
27,258 PointsThank you very much. I worked on that one for too long and couldn't get it. Your answer was "spot on"!