Ruby Objects3:42 with Jason Seifer
When instantiating a new instance of a class, that new thing is called an object. In Ruby, everything is an object. In this video, we'll explore objects and start to interact with them.
Instantiate an instance of the
string = String.new
View the methods the object supports:
Ask the object if it responds to a given method:
You may have heard the term object-oriented programming used before. 0:00 But what does that mean exactly? 0:04 As we learned earlier, we can instantiate classes. 0:07 When we instantiate a class, we're creating an instance of that class. 0:11 That instance is called an object. 0:16 When we design programs around the paradigm of classes and objects. 0:20 That's called object oriented programming. 0:24 The process of figuring out how to represent real life items as classes and 0:28 objects is called domain modeling. 0:33 But that's not really important right now. 0:36 You've probably heard of Ruby being called an object oriented language. 0:39 That's because in Ruby, 0:44 unlike some other programming languages, everything is an object. 0:46 When you have an object, 0:51 which is just an instance of a class, you can ask it questions about itself. 0:52 Let's see how that works now, using workspaces. 0:59 So let's go back into irb and take a look at an object. 1:03 So I'm going to click down into the console here and type i-r-b. 1:08 Now, I'm going to create another string. 1:12 Let's say string equals string.new. 1:15 Now string is our object. 1:19 So once we have an object, we can ask it questions about itself. 1:23 Let's take a look at the string documentation for just a moment. 1:28 [BLANK_AUDIO] 1:31 We can see over on the left here, there are a bunch of different methods. 1:33 [BLANK_AUDIO] 1:38 We can call any of these methods on our object. 1:41 So let's go ahead and look back at the string documentation so 1:46 we can see we've got this upcase method. 1:50 And the documentation says it's going to return a new string. 1:56 Now this isn't gonna do anything for us because the string is currently empty. 2:02 But we can see that it supports this method. 2:06 Now, since string is an object, and this is a method, 2:10 the string will actually keep all of the methods on itself. 2:15 Now, since string is an object, 2:21 we might not always know that it is of the type string. 2:23 And we also might forget what sorta methods string responds to. 2:27 We could get a print out of these methods by calling the method named methods. 2:34 That's a little bit confusing, but it does what you would expect and 2:42 returns to us a list of all of the different methods that string supports. 2:46 They also noticed that this returned an array, 2:54 which means we could call array methods on that. 2:59 So I could say string.methods.sort and 3:03 I would get an alphabetized list of all of the methods that this object supports. 3:05 Now if I wanted to know whether or not a certain method was 3:12 supported that could ask the string if it responded to this method. 3:18 So let's try saying string.respond to and 3:25 then I can pass in the name of the method I want to see if it responds to. 3:30 So we'll pass in upcase. 3:37 And this returns true. 3:39
You need to sign up for Treehouse in order to download course files.Sign up