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 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!