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

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Fill out the parse_answer method to return the answer passed in. If the kind is "number", convert answer to an integer u

def parse_answer(answer, kind="string") end

Theres alot of problems im having here. For starters im trying to figure out whats the purpose of this function. Second whats the kind variable in the arguements purpose. And three what am i returning?

ask.rb
def parse_answer(answer, kind="string")
end

2 Answers

William Li
PRO
William Li
Pro Student 26,799 Points

Hi there,

The return value of this function is just the 1st function argument answer. Only on the special case when the 2nd argument kind equals to "number" would you need to convert the answer into an integer before returning it. It makes sense to use if...else... clause here. Here's one way to go about solving it.

def parse_answer(answer, kind="string")
  if kind=="number"       # check if kind is "number"
    return answer.to_i    # if so, convert answer to integer and return it
  else
    return answer         # otherwise, simply return the answer variable.
  end
end

Hope it helps.

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

I guess the part that loses me is the number string. Like how is it gonna expect an output of a integer if the kind variable is a string? also the if "number" string throws me off completely because if its a string then how does ruby know to check for a integer?

Tiru Otilia
Tiru Otilia
2,274 Points
def parse_answer(answer,kind="string")
answer=answer.to_i if kind=="number"
return answer
end