Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Loops Build a Simple Contact List Part 1: Asking Questions

Sandra Grassl
Sandra Grassl
6,933 Points

Methods that return a value Code Challenge/Ruby Loops

In that code challenge I get asked 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.

This is the given code:

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

As this is very similar to the previous video I thought I input: def parse_answer(question, kind="string") answer = gets.chomp answer = answer.to_i if kind == "number" return answer end

But I don't pass the challenge with this. Can anybody explain to me what this challenge is expecting?

Cheers

4 Answers

The question isn't written that well, actually, because it should say 'if kind is "number"', not 'if kind is number'.

'answer' is an argument and so is 'kind' (set to "number" by default).

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

If you don't fully understand what's going on here, post it up and me or someone else will help you out.

Jitli Gan
Jitli Gan
2,668 Points

Why didn't you return the value for answer?

Oğulcan Girginc
Oğulcan Girginc
24,848 Points

Jitli Gan

You don't need to write return. It will automatically return answer anyway.

Sandra Grassl
Sandra Grassl
6,933 Points

Thank you. Now I agree, that this task is not written that clear.

I agree as well, this was the first Treehouse exercise I was stumped on. Judging from what they just taught us in the previous lesson, I would expect the code to look more along the lines of:

def parse_answer(answer, kind="string") puts "Enter something, anything: " answer = gets.chomp answer = gets.chomp.to_i if kind == "number" return answer end

thanks!