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

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.