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

Code is OK but still won't pass me

I have to 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.

What am I doing wrong, I've also tested it in my Console and it runs ok... I can't seem to understand the requirement, it's a little vague, return what ?? The answer or the kind converted to_i ??

ask.rb
def parse_answer(answer, kind="string")
  print "Your answer is: "
  answer = gets.chomp

  answer = answer.to_i if kind == "number"

  return answer
end

puts parse_answer(32)

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Minca,

The challenge is assuming that the answer has already been provided from the console, it just needs to parse the answer for the given kind.

You can remove the puts and chomp lines and simplify the function to the following.

def parse_answer(answer, kind="string")
  answer = answer.to_i if kind == "number"
  answer
end

I hope this helps.

Oh, I get it now! But please modify the requirement to say "assume that the answer has been passed already" :smile: it's a little tangled

Travis Granger
Travis Granger
2,745 Points

I don't understand why we don't need the "return" between lines 2 and 3 in @Justin's code.. would appreciate an explanation.

Thanks!

Justin Horner
Justin Horner
Treehouse Guest Teacher

Hello Travis,

The reason the return is not needed is because Ruby supports implicit return. In Ruby, the last expression evaluated is used as the return value.

I hope this helps.