1 00:00:00,180 --> 00:00:04,200 A common idiom that Ruby programmers use when writing classes and 2 00:00:04,200 --> 00:00:07,190 instances is to use the to_s method. 3 00:00:08,260 --> 00:00:10,220 This method is a convention, and 4 00:00:10,220 --> 00:00:14,630 is called by Ruby when an object wants to be formatted as a string. 5 00:00:16,000 --> 00:00:19,689 Let's see how that works now with the name class using workspaces. 6 00:00:21,655 --> 00:00:24,410 Okay, so here's our name class and 7 00:00:24,410 --> 00:00:29,450 it's just like before except I took out the attribute reader for 8 00:00:29,450 --> 00:00:32,440 first and middle name and put it back to what we have before with full name. 9 00:00:33,700 --> 00:00:38,590 Now let's take a look at what happens when we print out just the name so 10 00:00:38,590 --> 00:00:42,174 I'm gonna write ruby name.rb. 11 00:00:43,720 --> 00:00:48,820 And we see that we get this funny little format right here, 12 00:00:48,820 --> 00:00:51,650 which is the name of the class and its address in memory. 13 00:00:53,210 --> 00:00:59,461 Whenever we print something out to the screen using the puts method, 14 00:00:59,461 --> 00:01:04,221 internally Ruby is calling to string on it or to_s. 15 00:01:04,221 --> 00:01:07,203 And we can override that in our classes if we want to. 16 00:01:07,203 --> 00:01:15,227 [BLANK_AUDIO] 17 00:01:15,227 --> 00:01:19,710 So if we look here, we've got this to_s method. 18 00:01:19,710 --> 00:01:23,093 And let's just have this print out the full name with the title. 19 00:01:23,093 --> 00:01:28,888 [BLANK_AUDIO] 20 00:01:28,888 --> 00:01:32,430 Now when to_s is called is should print out the full name. 21 00:01:34,310 --> 00:01:34,980 And there we go. 22 00:01:36,130 --> 00:01:40,030 It can be very useful when you're working with your own classes to 23 00:01:40,030 --> 00:01:41,787 define a to_s method. 24 00:01:44,090 --> 00:01:46,780 Another method that we could use if we wanna find out 25 00:01:46,780 --> 00:01:51,100 a little bit more about the object we're working with is the inspect method. 26 00:01:53,250 --> 00:01:59,450 Inspect will tell you the internal state of the object that you’re working with. 27 00:01:59,450 --> 00:02:04,000 So here we can see the same thing as before, it’s the name class. 28 00:02:04,000 --> 00:02:06,490 And the place in memory or the object ID. 29 00:02:07,710 --> 00:02:13,410 And we also have a listing of all of the internal instance variables 30 00:02:13,410 --> 00:02:16,600 of the class as well as their values.