Tip:
Highlight text to annotate it
X
And talk a little bit about. >> [inaudible] and Ruby. And I think it's
easiest in this case to just walk through an example of how you do simple classes in
inheritance. So, we're going to divide a new class. It's going to be for a bank
savings account and an inheritance from account. That's what the less than
notation means. The instructor has to be called initialize. It's just one of those
things. Get used it. Even though the way you call it as we'll see it's with new.
It's got to be called initialize. It's an idiosyncrasy. Sorry about that. You can
pass the instructor any arguments you want. And in it we're going to set this
instance variable. Notice the [inaudible] sign. It means I?m an instance variable.
Alright. That is a [inaudible] thing. Okay, why do we need an instance method
whose sole purpose in life is to return the instance variable. It is because in
ruby the encapsulation of objects is total when you declare an instance variable like
at balance it is absolutely invisible anywhere outside the instance of the
object that owns it. Anywhere outside of that even in the same class. So the only
access to this instance variable from outside of the object is by defining a
getter method, as java calls it, some people also call it an accessor method.
And similarly we have to define a setter or a mutator method. Because the only way
to set the value of the variable from outside the object is to call a method.
Notice that balance equals is a legitimate method name. Okay, method names ending in
equal are special. It means I am a setter method. This is the only way to set the
variables value from outside the instance that owns it. We can deposit stuff into
our bank account, just by increasing balance. Okay, notice that inside this
method we can access that balance because we're still inside the object whose
instance methods these are. What about, [cough], excuse me. What about class
variables? Double at sign is what job I would call a static variable. It means
there's one value for all instances of the class. They can all access it, they can
all change it, they can all see the same value. And we can define class methods
which are often called static methods by proceeding the method definition with
self. Now as an aside and kind of as a preview, don't worry if this makes your
brain hurt a little bit, if it seems arbitrary that method name or the class
name method name begins with self, what's the value of self here. Well, as it turns
out when I say everything is an object in Ruby I really meant it okay, even a class
is an object. In fact a class is an instance ready, a class is an instance of
the class, class with a capital C. Okay, don't worry about that too much, but it's
basically my way of saying at least the language is internally consistent. When
you start, when you say class savings account, this is not a declaration in the
Java sense. There is a method called class that is getting executed and is actually
adding all this code at run time to define the methods on your class and within the
scope of this class. Non-declaration. The value of self is the class object that is
being created. So once you realize that, it makes a lot of sense that self.bankname
is a class method. It's defined on the object which is the class called savings
account that we're in the middle of defining. And what are we going to do in
this instance method? We're just going to return the value of the class variable
because again, it's invisible outside the class and there's a, if you can hold your
questions for just a couple of slides, that would be great. Okay. And I actually
have this in Pay Spin because we're gonna play around with it a little bit. >>
[cough] >> There it is. One click copy and paste. I hope you've all been doing this,
because the one click copy and paste is super cool. Okay, so now we're gonna we're
gonna use that to do a few simple tricks just a few minutes down the road. I could
also, by the way define this class method. Instead of self.bankName I could have said
defSavingsAccount.bankName. Again, once I've started defining things in this
class, really what's happened is Ruby has created a new constant savings account
which is the class object, so I could just as well say this is a method that's
defined on that object whose name is that constant. Again, don't worry about it too
much. This'll, in like a week, this'll be super clear. For now just kind of look,
absorb, remember. Okay, now you would think that given the instance variables
are so popular there would be a short cut, where you say G, you know I want a simple
instance variable where you may have to just define these trivial access methods
and set of methods just so that I can get access to it from the outside world. You
could do that or [inaudible]. >> Mm. Let's see that again. >> [laugh] >> You could do
that. >> [laugh] or you could do that. Clearly much nicer and much drier. Now
what's going on here? [cough] attr accessor, again, is like so many other
things we've seen, not part of the language. It is just a plain old regular
method that uses metaprogramming. Programming at run time, defining new code
at run time. To create those methods for you, on the fly, right? It's basically
taking the thing that you passed, the symbol balance and it's using that as a
template to define these methods on the fly and to provide them for you. So it
really is, in that sense, just saving you some typing and a fair amount of
readability. But there's no magic in this, right? It is just a regular method that
you could write yourself. In fact for homework one, you will do so, if fact you
will improve upon it. Okay so. With that in mind, here's where we are so far. Ruby
is object oriented and there's no multiple inheritance. There's a far cooler thing
than multiple inheritance which we're gonna meet shortly which is called a
mix-in. Everything is an object and the only you get things done is calling
methods on those objects. So class and incidence variables are invisible outside
the class, you've gotta call a method to do anything to them. Everything is a
method call, and as we'll see over and over again, I'm gonna hammer this; you
only care if the receiver responds to the method. This is very different to asking,
is the receiver the right class or the right subclass? Most of the time that is
irrelevant. The only question is whether it responds to the method, and we're gonna
use that to do some really, really nice programming. Most things that look like
operators aren't, they're actually instance methods, and they may be defined
different ways in different classes. Objects do have types, but the variable
that refer to them don't have types. And although the vast majority of methods are
not destructive, there are a few that are. Double less then is one example and
there's few where there's a destructive variant that ends in an exclamation mark.
Right, the exclamation mark is a way of warning you that this might be a dangerous