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

Devin Scheu
Devin Scheu
66,189 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
MOD
Sean T. Unwin
Treehouse Moderator 28,388 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. :)

J V
PRO
J V
Pro Student 16,607 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
PRO
Anand Mohan Duddella
Pro Student 8,263 Points

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

Shaimoom Newaz
Shaimoom Newaz
7,219 Points

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)