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 trialJohn Fisher
7,974 PointsI don't get "kind"
I know this has been asked before on the forum, but I still dont totally get it. Where did "String" and "number" suddenly appear from, and what are they doing?
Is "string" and "number" some sort of preset thing?
def ask(question, kind="string")
print question + " "
answer = gest.chomp
answer = answer.to_i if kind == "number"
return answer
end
11 Answers
rowend rowend
2,926 PointsWell, let me say this: The ask method get 2 arguments. The first argument question is not optional. The second argument kind is optional and have a default value 'string'. This means you can call your method like:
ask('what is your name?') #in this case the method will no do the integer conversion and will return an string.
ask('waht is your age?', 'number') #in this case the method will do the integer conversion and will return an integer.
I hope this help you.
Christopher Phillips
10,061 PointsIn the context of what has been taught in the Basic Ruby course so far, it was unwise to suddenly dump "kind" on us without any previous explanation . The examples should only rehearse what we've learned so far otherwise it's distracting and confusing.
Ulfar Ellenarson
5,277 PointsHi
I agree this was not discussed nor does the instructor shed any light onto why he is using kind as an argument. What is noticeable is that you can comment out "# answer = answer.to_i if kind == "number"" and the code still works. You can also add p.answer.class and see that whatever value you put into the answer it is always returned as string because you entered a string.
contact_list = []
def ask(question, kind="string")# kind = "string" assigns "string" to the variable kind. kind == "number" checks the variable kind to see if its value is equal to "number".
print question + " "
answer = gets.chomp
# answer = answer.to_i if kind == "number"
return answer
end
answer = ask("Whats your age?")
puts answer
p answer.class
./contact_list_step1.rb
Whats your age? 44
44
String
Nick McDaniel
962 PointsThese classes kind of suck - I like that they're going for ease and simplicity - but part of simplicity means building up each brick of the code house, explaining each part of the code and it's general use cases/how it works, before setting the code "brick" down as part of the larger structure being built. Throwing this random brick confused the heck out of me. Look how bad there moderation is too. Not one response explaining what this is, three years later.
Trial, over. I'll check out another site.
John Fisher
7,974 PointsI dont understand exactly why or what they are doing in this particular example. What is their role?
Ron L
6,463 PointsI agree with you completely and am just as confused about this. It wasn't explained very well.
Caleb Kleveter
Treehouse Moderator 37,862 PointsThis Treehouse forum post might help.
SZE XU
1,053 PointsI didnt get this as well........
Nick McDaniel
962 PointsBefore I change, I'd like to give the mods a response to respond. Look at this code. Shouldn't this change the output to "integer" in this case? If not, my guess of what this code does, (keyword, guess) is off.
def parse_answer(answer, kind="integer")
if kind == "number"
puts answer.to_i.class
else
puts answer.class
end
end
parse_answer("5")
[when tested, the output is "String"... double-you-tee-eff mate!]
Enrica Fedeli-Jaques
6,773 PointsThe first 'kind' sets a default value: if only one argument is passed, then the kind (of that argument) is automatically assumed to be string. the second kind says if there is a second argument passed and that second argument is a number, apply .to_i (I believe this is because if you type 3, 5, 7 whatever number, that would be considered a string by ruby unless you tell it to convert it to an integer, or maybe because that number could also be a float?) . I'm pretty new to coding, hope it's right and clear :)
Caleb Kleveter
Treehouse Moderator 37,862 PointsIn a workspace?
Caleb Kleveter
Treehouse Moderator 37,862 PointsAre you saying that you didn't learn this code before?
Lian Maung
4,611 PointsLian Maung
4,611 PointsI am disappointed for the fact that the instructor does not normally insert reasonable explanations as to why codes are written in certain way and what is their purpose. They teach us what to know not how to know. For this case the instructor doesn't explain why we need to set a default value at all. For example, in this kind == "number", number is string and does Ruby automatically understand the input as number or not?