Kernel and Object6:32 with Jason Seifer
The Object and Kernel classes are the root of all Ruby class and object functionality. Object is the base class that all Ruby classes inherit from. The Kernel module is included by the Object class and contains methods that can be used in all classes.
The Object and 0:00 Kernel classes are the root of all Ruby classes and Object functionality. 0:01 Object is the base class that all Ruby classes inherit from. 0:07 The Kernel module is included by the Object class, and 0:12 therefore contains methods that can be used in all classes. 0:16 Let's go ahead and take a closer look at Object and Kernel now using workspaces. 0:21 Now here's the documentation for Kernel, and 0:27 then in this other tab, we have the documentation for Object. 0:30 Let's go ahead and take a look at Kernel first. 0:34 Now the documentation tells us that the Kernel module is included by 0:38 the class Object, so its methods are available in every Ruby object. 0:43 Now, we can find the instance method documented in the Object class, but 0:49 the module methods are documented in the Kernel class documentation. 0:53 Since they're module methods, and Kernel is included, 0:59 we have access to all of these basically anywhere in Ruby. 1:03 And here we can see just some of the methods that we have available. 1:09 For example, Array will return any argument as an array. 1:12 So let's go ahead and try this in an IRB prompt which I have up over here. 1:17 We'll say, Array. 1:23 And just pass in one argument here. 1:24 1, and it returns an array. 1:28 Same thing if we pass in a string. 1:33 Now, that's not a very interesting method, but we can see that that works with array, 1:36 complex, float, hash, integer, rational, and string. 1:41 So we could pass in an object and it would be returned as a string. 1:47 Now some of these other methods in here will tell you the name, 1:52 for example, of the current method as a symbol or nil. 1:58 We also get methods like block_given which we'll send into any method. 2:09 You can say if there is a block_given go ahead and work with that. 2:15 Or if not, do something else. 2:19 But let's scroll down a bit to some of the interesting methods that we've 2:22 seen before. 2:25 For example, here is the puts method. 2:31 Now, we've seen that several times. 2:36 And that allows us to print something to the screen, like Hello. 2:38 And it also returns nil. 2:43 Now, what's interesting about this is this is part of the Kernel module. 2:45 And we have seen other ones before. 2:51 Printf, puts, print, and p as well. 2:54 Now, if we take a look at the Object class, here, 3:00 we have access to some parts of the Kernel module. 3:05 Now, here's something that's interesting. 3:09 Object is the default root of all Ruby objects. 3:11 So every object is inherited from Object or BasicObject. 3:15 Now when we're working with classes, we can ask the class what its superclass is. 3:21 So for example, we could say String, which we're used to, and 3:28 ask what class that inherits from with this method superclass. 3:34 We can see that that inherits from Object, which means that strings are going to have 3:37 access to all of Object's methods, and Object includes Kernel as well. 3:43 So there are some interesting methods that we have inside of Object. 3:49 For example, the methods method returns a list of 3:54 all the public and protected methods of an object. 3:59 So for example, we could have a new string, And 4:08 ask this string what its methods are. 4:13 And this will show you everything that String supports, and it returns an array. 4:17 So since it's an array, We can sort it, and 4:24 that will now be in alphabetical order. 4:28 Now, if we go back and look at the Object methods, 4:32 Here's an interesting one called respond to. 4:39 And what respond to will do is tell you if an object responds to something. 4:42 And when we ask if it responds to something, 4:48 that will mean that any of these methods work on a given object. 4:51 So, for example we could say, 4:55 does this string respond to strip? 5:00 Now, we saw that in the methods, so it returns true. 5:06 But if we made something up, it will return false. 5:10 Now, we can ask any object for these things, 5:15 because all objects descend from this Object class. 5:19 Which is also where we find this nice inspect method. 5:25 Inspect is a default on pretty much any class, string.inspect. 5:29 And it's nothing. 5:35 Just an empty string right here. 5:36 The backslashes indicate an escape character. 5:38 If we had had our own class, it would print out something like this. 5:41 We can ask classes if they are an instance of a certain class. 5:47 And that will return true. 5:57 And for example if we look at a string and ask if it's an instance of Array, 6:03 that will be false. 6:06 There are a lot of methods here inside of the Object class and the Kernel module. 6:11 Go ahead and look through the documentation and 6:18 try some of them out now in your own workspace. 6:20 We didn't cover all of the Kernel methods in this video. 6:24 Try experimenting with some of them on your own now using workspaces. 6:27
You need to sign up for Treehouse in order to download course files.Sign up