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!

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

Andrew Carr
Andrew Carr
10,979 Points

Why if function does not work.

I tried using the following code but it doesn't seem to complete the code challenge. I understand "answer = answer.to_i if kind == "number"' is simpler, but how is my code inherently different?

Thanks!

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

1 Answer

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

You are comparing the value in the kind method on answer to the string "number". The answer object that is being passed into parse_answer may not have a kind method.

The construction of the parse_answer method is looking to get at least 1 required argument (answer) and 1 optional argument (kind).

You need to test the value of the optional kind argument to pass.