[music]. >> I've said a few times now that in Ruby everything is an object, and here I want to focus a little bit more on what I mean by that, and show you some related idioms that work well in Ruby. So Ruby is fully committed to the fact that every result of every expression is a value. This leads to a smaller, more regular language than if you have some exceptions to that rule. Like oh, except for numbers, or oh, except for some special null value, or something like that. And so as a result, you can call any method on any object. Now, that object may not have that method defined, in which case you'd just get an error that the method is undefined. Actually it's even a little more interesting than that. What actually happens is the Ruby interpreter, the Ruby implementation says, oh, that object doesn't have that method? I will call it's method missing method instead. And it turns out that the built-in method, missing method, that by default, every class has defined, prints out that error message. We've also already seen things, that, like, almost everything in Ruby is a method call. We've seen that when we call addition that's really just sending the plus message. And things like that. So let me show that with numbers, because they're a good example. So I can, you know, of course, I can write 3 plus 4 and get 7. I can also write 3 dot plus and then parentheses 4, because that's just the version without syntactic sugar. I could also say 3 dot abs which is, the absolute value method. I've shown you that before. There's a method called non 0, that, just returns the number itself, unless you, have the value 0, and then, of course, you get nil. And of course, we don't have to do this on integer constants. You know, we could have a variable that holds something like, if 3 is greater than 4 then 5, else negative 5 end. And you know, x is minus 5, and x dot abs is 5, and so on. All right? So that's kind of a pure object-oriented programming with numbers. The next thing I want to talk a little bit about is this value nil. So nil is used in Ruby as sort of the object you use when you don't really have any data. So it's not unlike ML's unit or null in Java or C or C++ or C#. But it is an object, and that's the key difference. So I could call nil with various methods. Now, it doesn't have a lot of methods defined, but it does have a nil question mark method that it returns true for. Everything else returns false. You know, a string is not nil, and so on, not even the empty string. Okay. But nil is nil question mark. Nil has a bunch of other methods as well. I will point out, by the way, that in Ruby, the nil object counts as false. So there are two things in Ruby that are false, the false constant, and the nil object. So, indeed, if I said, you know, maybe y equals if 3 is greater than 4, which it's not, then nil else 42n, so y sorry the other way, how about 4 greater than 3. There we go. Get true, now y is nil. And now if I said, if y then put s A, else put s B, then indeed, it printed out B, because y the nil object, anything that evaluates the nil counts as false. So that's interesting . And, and so on and so forth. Everything is just method calls. So now what I want to emphasize is that all the code in our program is just methods. Everything is a method of some class, all right? And then you call that code by making an instance of the object and calling it on that class. So, top level methods, you might think are somehow different. But it turns out they're not. If you put a top-level method outside of any class definition, in a file or in the REPL, then it just gets added to the object class. Now, the object class is something that is a super class of all other classes. Now we haven't talked about subclassing yet, we'll study it explicitly a little bit later in this section. But what basically happens, for the purpose of this discussion, is when you define a class, you also get in your class, all the methods of all your super classes. So, since object is a super class of all the classes you define, you get all of its methods, as long as you don't replace them with a method of the same name. So, those top-level methods are now just methods of every object, because they're in every class. So again, it's just object oriented programming. It's just a way of thinking about top-level methods that is more OOP style and fits in to the sort of this small Ruby language. So the last thing I want to emphasize in this segment, is that this thing called reflection and how it can be used for exploratory programming. So those are big fancy words for pretty simple idea. It turns out that there are other methods that are defined on all objects in Ruby, that tell you things about the object like, what methods it has, and what class it is an instance of. And if you use those methods at runtime, while your program is running, you're doing a thing that's often called reflection. What you're doing, is you're learning things about the program while the program is running, like, what is the class of this object? That has some uses in programming. We will generally avoid those uses. There is often, but not always, a better way to do the same thing. What I thought I would show here is that it's sometimes useful in the REPL just to explore your program, debug it and learn more things about it. And it's nothing more than method calls on objects. So, for example, if I say 3 dot methods, it turns out I get a very large array of symbols. We're going to learn arrays soon, but these are all the methods you can call on 3. Now some of these have to do with arithmetic, like 3.succ returns 4. That's the successor method. 3.even returns false and so on. Some of these have nothing to do with numbers. They're just methods that were defined in object, or some other super class of the class of 3. So for example, methods, right, that has nothing to do with numbers per se. Here's something cute you can do. We could also find out all the methods of nil. So, it turns out there's quite a few methods, on this thing that doesn't even have any useful data. And then, it turns out that arrays support the subtraction operator, so here are all the methods that are part of 3 and not part of nil. And now you'll notice if you look at all these, that almost all of these have to do with some kind of arithmetic, or have something to do with numbers. There might still be a couple exceptions in there. The one other thing I showed promised you for reflection is class. So it turns out that 3 is an instance of the fixed num class. So when you call the class method you get that back. You might be curious, wait a minute. Is fixed num an object? Does it have methods? Yes, it does. What is its class? Turns out it's class. So, even classes are objects that have a class. I could have seen the same thing by just writing 3 dot class dot class, because that's just this. Right then you might ask what's the class of class? I mean, you almost can't help yourself, right? And it turns out it's also class. So that's a fairly interesting object whose class is the object itself. Herein lies the road to madness. People love to ask questions about what's the class of a class of a class. But nonetheless, it's an interesting example of this sort of exploratory programming. I often find it convenient to look up the methods of an object or a class, just here in the REPL, but then I usually have more questions, and I head off to the documentation online to find out more of the details. And that, hopefully gives you a little bit more of a sense of how in Ruby, really, everything is an object and all code is a method of some class.