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 trialErik Nuber
20,629 PointsCode in video does not work as intended
The following code as given in the video for this section does not work as it is intended. If anything it just adds unwarranted confusion as can be seen in the questions asking what kind="string" and kind=="number" are doing.
The issue I was trying to figure out is that each time the ask method is called, there is only a question argument passed in. Thus kind is always a string and never equal to number so nothing will ever be set to an integer. So when asking the question if a phone number should be added it does not give the results one would expect. The phone number forever remains a string and will include any set of letters and numbers. I have not finished the series of videos and it may be corrected later but, those searching for an answer the problem is in the code itself.
I added a comment to the code that should fix things properly. Also, just to note, you can test it yourself but, when entering a phone number any combination of letters and digits will have the following effect. If the number starts with numbers and has letters after or between any numbers it will convert to just the first set of numbers before for the first letter. If letters and then numbers are put in, it will set the value to 0.
note: I added in a little bit of code that tests things while I was trying to figure out what was going on.
contact_list = []
def ask(question, kind="string")
print question + " "
answer = gets.chomp
answer = answer.to_i if kind == "number"
puts answer
return answer
end
def add_contact
contact = {"name" => "", "phone_number" => []}
contact["name"] = ask("What is the person's name? ")
answer = " "
while answer.downcase != "n"
answer = ask("Do you want to add a phone number? (y/n) ")
if answer.downcase == "y"
phone = ask("Enter a phone number: ") #add , "number" as an argument here will fix things
contact["phone_number"].push(phone)
end
end
return contact
end
answer = ""
while answer.downcase != "n"
contact_list.push(add_contact())
answer = ask("Add another person? (y/n)")
end
puts contact_list.inspect
2 Answers
Erik Nuber
20,629 PointsAppreciate any response. If you are going thru the Ruby course yourself please look for my questions as you go. I have not gotten any responses for anything.
I just wanted to mention that I do understand what you are saying however, that wasn't the point of my original post. The videos in this section use a second argument in the "ask" method that either shouldn't be there and; the code should be cleaned up to remove kind everywhere or, it should have been used as intended. The code does work but, I would consider it to be dirty and in need of fixing.
The second argument simply makes the variable kind a string if no second argument is passed in. This would be no big deal at all however, a couple of lines later the instructor gives an if statement based on kind being equal to number. That never happens in the course so will never be true. Thus again you could either remove that line altogether or fix it by adding in the second argument "number" where I marked in the code.
For those just learning a language or trying to learn a new language, using pieces of code that are incorrect or unused just adds to confusion. This is evidenced by all the questions regarding kind = "string" and king == "number."
This is not the only series of videos in this particular course line that use code that needs small corrections but, I do understand that ultimately what you are being taught are the concepts.
Chris McKnight
11,045 PointsYou are describing the behavior of String#to_i. From the documentation, String#to_i will return 0 when the start of the string is not a valid number.
I would actually strip out everything that isn't a number using a regex.
My Philosophy: Don't convert data types unless absolutely necessary. In other words, why would you convert a phone number to a number anyways? A phone number is just a string of digits (contrary to what the name says). Typically, you don't need to do math to a phone number, so, just leave it alone.
Chris McKnight
11,045 PointsChris McKnight
11,045 PointsAgreed
I am not going through the course. I have been a developer for 7 years and have been writing Ruby code for 3 years. ;)