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

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
PLUS
William Li
Courses Plus Student 26,868 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