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

Ali Amirazizi
Ali Amirazizi
16,087 Points

What do does (kind = "string" and kind == "number") mean?

I don't understand how these statements work. Is the word "kind" a keyword? Also how does putting the word "number" in quotes check to see if it's an integer thats being passed, since its in string quotes.

Ali Amirazizi
Ali Amirazizi
16,087 Points

Hi, thanks for your response. I'm slowly wrapping my head around the logic. Here's the code snippet for context.

def ask(question, kind="string")
  print question + " "
  answer = gets.chomp
  answer = answer.to_i if kind == "number"
  return answer
end
  1. Can you explain how a variable declaration is used as an argument in line 1.
  2. For the statement (answer = answer.to_i if kind == "number"), it is saying the users input(which is a string) will change to an in integer IF the value of 'kind' is equal to the string "number". I guess what I'm confused about is how ruby evaluates the string "number" and "string".
Ali Amirazizi
Ali Amirazizi
16,087 Points

I think I figured it out. The function works to change the users string input into a number(if they enter a number) because anything inputed is evaluated as a string. Thereby, if a number is inputed, it is originally a stringy number which then changed and evaluated as a number.

4 Answers

Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

in the first line

def ask(question, kind="string")

kind="string" means that the function accepts an optional parameter named kind so if you don't specify it, it gets the value of "string".

for example:

ask("Are you ok?")

it is converted internally into

ask("Are you ok?", "string")

if you write instead this:

ask("Tell me your lucky number","int")

kind parameter will be = "int"

Thas Eagans
PLUS
Thas Eagans
Courses Plus Student 2,533 Points

I'm no Ruby coder, but if its like any of the other languages I've worked with:

kind = "string" assigns "string" to the variable kind. kind == "number" checks the variable kind to see if its value is equal to "number".

</TREagans>

Jitli Gan
Jitli Gan
2,668 Points

Can someone explain again? I don't quite understand this.

Try adding the string "number" to the function in your workspace:

answer = ask("What is your name?", "number")

the code will now return integers. even if you enter a string, it will return a value of zero.

i add int values and returned to 0 but when i leave it into string it works well!