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

Clifford Stiber
Clifford Stiber
1,343 Points

Not sure why (answer, kind="string") is required if we add the line answer = answer.to_s if kind = "string", seems redun

Can someone please clarify this for me? Not sure why (answer, kind="string") is required if we add the line answer = answer.to_s if kind = "string", seems redundant?

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

ask.rb
def parse_answer(answer, kind="string")
end

2 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! This is tricky. It's because we could call the method like this:

parse_answer(my_answer)

And you'll notice we send off one argument... not two. When we do this then it will automatically set kind equal to "string" as the default value. But we could call it like this:

x = "number"
parse_answer(my_answer, x)

At this point kind will now be equal to "number" instead of "string". The kind="string" is just there to tell us what to default to if nothing is passed in. Hope this helps! :smiley:

Clifford Stiber
Clifford Stiber
1,343 Points

Thanks.. Really appreciate the speedy reply.