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

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

Ruby override method - contact class: Part 2

Hi, everyone, in the Contact Class: Part 2 | Build an Address Book in Ruby from minute 1:52 , I'm completely lost. I watched the video so many times, I understand what Jason's aim is but I don't think we covered override methods, and I'm completely lost from there on. OK he is passing the 'full_name' format argument as a default parameter, I get that, and then he writes the case statement to tell the program what to do in each other cases different from full_name. Why does he need to override the to_s method? why does he put ' ' around the variable names? Then he calls the method passing the arguments (again I don't get the ' ') anyone that can try to explain it to me? apologies it's if what I'm asking is not clear and I know it'll take quite a bit of typing, not the shortest answer. TIA!

https://teamtreehouse.com/library/build-an-address-book-in-ruby/class-design/contact-class-part-2

2 Answers

Neil Northrop
Neil Northrop
5,095 Points

Hey Enrica!

I'll try to answer your questions the best I can and see if I can help out. I'm sorry I don't have a full subscription so I cannot watch the full video to get the full context of why Jason is doing what he does but again I'll try to answer the best I can.

Why does he need to override the to_s method?

Jason is probably overriding the to_s method to help teach some points of programing, I can't watch the full video so I'm not sure of the why. But the to_s method is given to us for free from the Object class. So if we want to use the to_s method differently we need to override the class by defining to_s in our class and coding it how we want. In this case, maybe we want the full name, the last name, or last first name printed out. Otherwise if we did not define to_s in the Contact class, it would do the default code from the Object class which is, "The default to_s prints the object’s class and an encoding of the object id." I hope that helps.

why does he put ' ' around the variable names?

This one might be a little harder for me to explain, but I think it comes down to not being able to pass method names to a case statement and it call the method that you're attempting to pass in. What Jason is showing you is more of a simpler way to help show how to override and choose which method you'd like to print out with the to_s method. A more advance approach to this would be to override the to_s method and pass in the method name as a symbol, like so.

def to_s(format = :full_name)
    self.send(format)
end

^^ That allows you to send a method name as a symbol to the Contact class. So you could do something like:

name = Contact.new
name.first_name = 'Foo'
name.last_name = 'Bar'
name.to_s(:last_first)
>> "Bar, Foo"

Both ways accomplish the same thing.

Let me know if any of that helps. If not, let me know and I'll try to help some more.

Thanks, Neil

Joe Sharp
Joe Sharp
19,791 Points

Since Neil wasn't able to say with certainty, the override method is actually to give the object a string-like method call but with slightly different behavior than the method typically provides. Normally to_s simply converts the object into text. Now our new version of .to_s by default converts the object into text as well, but now offers several formatting options with a default of what we typically would want to do. Everything Neil said is accurate, with one exception. He couldn't tell based on your question but the case statement calls the different text formatting methods based upon what we pass into to_s. The reason the method names (not variable names, the variable is format and being set to a string) are quoted is because if you were to pass the method names unquoted it would actually call the method and pass the result to to_s.