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 trial

Ruby Ruby Loops Build a Simple Contact List Methods That Return a Value

Devin Scheu
Devin Scheu
66,191 Points

Ruby 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:

ask.rb
def parse_answer(answer, kind="string")
  if answer = kind
    return answer
  else
    return answer.to_i
  end
end

4 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

You 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
Jessica H
16,612 Points

That'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
Anu Sandhu
2,719 Points

Thanks Jessica!

Anand Mohan Duddella
PLUS
Anand Mohan Duddella
Courses Plus Student 8,264 Points

try without answer = gets.chomp and try with giving == (kind == "number")

Can someone explain why we remove the gets.chomp? Doesn't that code allow us to "return the answer passed in."

Anu Sandhu
Anu Sandhu
2,719 Points

I 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

Try without gets.chomp I was confused also)