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 2: Adding Contacts

Justin Sze Wei Teo
Justin Sze Wei Teo
9,418 Points

Kind = "number" wasn't declared, so why did phone = ask("Enter a phone number:") work?

Hi guys,

I refer to the code below pertaining to the add_contact method,

def add_contact
  contact = {"name" => "", "phone_numbers" =>[]}
  contact["name"] = ask("What is the person's name?")
  answer = ""
  while answer != "n"
    answer =ask("Do you want to add a phone number? (y/n)")
    if answer == "y"
      phone = ask("Enter a phone number:")
      contact["phone_numbers"].push(phone)
    end
  end
  return contact
end

As per my title, why is it that the teacher did not type, phone =ask("Enter a phone number:", kind ="number") instead?

I thought we had to do this so that answer.to_i would be run in order to return the answer as an integer instead of the default string (since by default, kind="string", and would return just gets.chomp)

2 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Justin Sze Wei Teo , the kind parameter is only in the ask method so that the answer = answer.to_i if kind == "number" line can be run on the answer if the kind is "number". We actually don't want to treat phone "numbers" as numbers, we want to treat them as strings. If you run to_i on a phone number string like "(480) 555-1212", the return value will not be what you want:

treehouse:~/workspace$ irb                                                              
irb(main):001:0> "(480) 555-1212".to_i                                                  
=> 0

You only want to run to_i on strings that consist entirely of simple integers:

irb(main):002:0> "12".to_i                                                              
=> 12                                                                                   
irb(main):003:0> "-12".to_i                                                             
=> -12 

it's not necessary to even have the kind== "number" in the first place.

Correct, it's not for this example. I haven't reviewed the entire course but I assume that elsewhere the ask method is used to parse a simple integer string.

Justin Sze Wei Teo
Justin Sze Wei Teo
9,418 Points

Hi Jay,

That is extremely helpful! Thanks for taking the time to explain it.

That bothered me as well when I last followed that video. For the intent of showcasing the output, it works anyway because it didn't matter if the number was a string or integer. It displayed as intended.

Thinking about it now, I'm not sure if it's necessary to have it as an integer in the first place. One for formatting benefits of a string, and the other reason is that phone numbers are typically static. They don't change, but are replaced.

Justin Sze Wei Teo
Justin Sze Wei Teo
9,418 Points

Jay McGavren , any idea? David brought up some good points. Correct me if I'm wrong David but when you mean static do you mean that the phone number will not be manipulated , i.e., there is no xyz = phone + some random number .

Since we're just printing a result with no manipulation , it's not necessary to even have the kind== "number" in the first place.