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
Alphonse Cuccurullo2,513 Points
Does "number" in string form matter at all? check syntax
def parse_answer(answer, kind="string") if kind=="number" # check if kind is "number" return answer.to_i # if so, convert answer to integer and return it else return answer # otherwise, simply return the answer variable. end end
So this works what im wondering is why does it work? The kind variable in the arguement equals a string now the function says if kind == "number" then return answer.to_i what im lossed at is the "number" is a string so how is function gonna reconize my input of a integer?
Jason AnelloCourses Plus Student 94,610 Points
If I understand, I think your confusion lies mostly with what's happening with kind="string" I'll try to explain that and I think it would also help you understand this better if we look at some examples of how this function would be called.
The overall idea here is that we have strings like "hello" that can't be converted to numbers and we have strings like "534" which can be converted to numbers. This function shouldn't do anything with "hello" but return it. However, it should call the to_i method if it receives something like "534" and then return the number 534.
The second argument which is stored in the local parameter
kind is meant to tell the function which type of string was passed in as the first argument. If you pass in "string" as the 2nd argument then the function knows not to bother converting it. However, if you pass in "number" as the 2nd argument then it knows that it should convert to a number.
Regarding kind="string" -
This is a way to assign a default value to an argument. Let's suppose that the majority of the time we use this function we're passing in strings that can't be converted. Why should we have to always type in that second argument of "string"? By setting a default value of "string" on that second argument means we can leave it off most of the time. Then on the less frequent occasions where we're passing in a string that can be converted we'll go ahead and pass in that 2nd argument of "number"
Some example function calls with comments:
# this string can't be converted to a number so we pass "string" as 2nd argument parse_answer("hello", "string") # returns "hello" # However, "string" is the default value so we don't have to pass it in and it works exactly the same parse_answer("hello") # returns "hello" # this string can be converted so we must pass in the 2nd argument "number" so that the if block will be true and the to_i method is called parse_answer("5", "number") # returns 5
Let us know if that clears it up for you.
Francois van der Hoven2,026 Points
Francois van der Hoven2,026 Points
Hi Alphonse, from the information you have provided it is clear that 'kind' is an instance variable in that class. It's meaning will become clearer to you when you find out which values can be assigned to kind and under what conditions. The logic in the method, parse_answer, expects that, by the time this method is called, kind would contain a string value that tells you something the contents of the answer variable.