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 Build an Address Book in Ruby Class Design Address Class: Part 2

Ruby syntax error caused in dual to_s methods?

I have been following along with the lectures and checking my work. I do not see any differences between them but am suddenly getting errors on the to_s method in the contact.rb file where I had no previously had one before the address file was included.

I am not sure how to fix this? Any suggestions?

Here is the snap shot link of my code:


Here is the error that it is throwing:

contact ruby:69:syntax error. unexpected Tidentifier, expecting end of input, def to_s('full_name")

Thanks for any guidance!

1 Answer

Scott Wyngarden
Scott Wyngarden
Courses Plus Student 16,700 Points

The first thing is that you have a mixed set of quotation marks when you're requiring the address field:

require './address"

Next, there's an extra end in your print_phone_numbers method and also at the end of the file

def print_phone_numbers
    puts "Phone Numbers"
    phone_numbers.each { |phone_number| puts phone_number }

and lastly, you're calling add_address with 5 arguments instead of the 6 that are in the method signature

A number of those are tricky ones to see, because the line number of the error (like 69 in contact ruby:69:syntax error. unexpected Tidentifier, expecting end of input, def to_s('full_name")) come from where the interpreter first saw something that would throw an error, but not where the typo happened.

Thank you SO MUCH, Scott, for taking the time to respond to me. :)

Do you have any suggestions for someone new to better identify where in a program the error is thrown if Ruby only calls where the crash will happen rather than where the typo occurs?

Also, for this instance, I had thought the if !street2.nil? workaround built into the address file on line 9 would mean that a lack of entry for this variable would not cause a hiccup. Any suggestions on how to code that better or maybe I just misunderstood the purpose of the if !street2.nil? ?

Scott Wyngarden
Scott Wyngarden
Courses Plus Student 16,700 Points

Syntax highlighting helps a lot. It's what made me notice the single/double quote mixing, since none of the keywords were color coded the way I'm used to seeing.

I've found that it also just takes time and spending the effort to figure out what the (sometimes cryptic) error messages are trying to say to get a sense of what the underlying issue might be. Also, your post mentioned that adding the address.rb file was sometime near when things started to throw errors, so that could be a clue. Having in your mind known good states (which you'd get by running/testing your code often) makes it easier to figure out when you might have introduced an error.

the !street2.nil? works for figuring out whether or not someone sent you a nil value into method and adding/not adding a space and the value to the string you're building, but a caller still has to provide all the parameters asked for in the method definition/signature. In what you had set up, the interpreter wouldn't be able to figure out you meant to omit street2 when passing in 5 parameters.