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

soufiane oucherrou
soufiane oucherrou
614 Points

what's wrong with this code?

I tried this code on Atom and it works perfect but here gave me error. any suggestion ?

ask.rb
def parse_answer(answer, kind="string")
  print answer + " "
  sawel=gets.chomp.capitalize
  sawel=sawel.to_i if kind =="number"
return sawel
end
puts parse_answer("whats your name")

1 Answer

khenhey
khenhey
16,570 Points

Hi Soufiane,

I don't think there is a lot wrong with your code. I tried it out in my terminal by running irb and it is definately returning the answer. But I think the coding challenge expects something different, which is confusing, because you are doing it (almost) exactly like the teacher showed you in the previous video. So maybe this is a problem in the question itself that TreeHouse should look at sometime?

I saw someone else who was also struggling a bit with this. They eventually solved it by just doing a normal if-else to return the answer. Maybe try this? You can see their answer in this post: https://teamtreehouse.com/community/methods-that-return-a-value-code-challengeruby-loops

Jay McGavren
Jay McGavren
Treehouse Teacher

Agreed, there was a lot of potential for confusion there. I have updated the challenge instructions as follows:

Your goal in this challenge is to write a method named parse_answer that works similarly to the ask method in the preceding video. But parse_answer will work a little differently than ask.

The parse_answer method should not display any prompts or read any keyboard input (so, you won't be calling the gets method at all). Instead, the user's input will be recorded for you elsewhere in the program, and passed into parse_answer in the answer parameter.

Your parse_answer method should look at the kind parameter. If kind equals the string "number", it should convert the value in answer to an integer and return that integer. If kind is any other value (including the default value of "string"), then parse_answer should return the value in answer without making any changes to it.