Enter your Sign on user name and password.

Forgot password?
Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Classes: Part II

  • Class methods are invoked by the class itself
  • To create it, prefix the name of the method with the name of the class
  • You can define a array and hash access method using []
  • It allows you to define your own hash access in a class
  • The each method allows you to create your own iterator
  • Use yield in the each method to mimic a loop
  • Defining an == operator allows you to create your own equality operator
  • Constants are public and can be called inside and outside the class

Classes: Part II

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Defining Operators 0:10
    • You can define arithmetic operators
    • Unary Operators
    • Let's define operators in our class!
    • Example
  • Class Methods 6:24
    • Examples
  • Opening Up the Class 9:38
    • Adds an additional method
    • Examples
  • Array and Hash Access Method 15:40
    • Use square brackets
    • Define your own has access method
    • Example
  • Enumerating The Values 18:40
    • Define the each iterator
  • Testing for Equality 19:36
    • Class Triplex
    • Examples
  • Constants 25:00
    • Usually defined at the top of class

Transcription: Classes: Part II

Welcome back to Educator.com.0000

Today's lesson is on classes; this is the second part.0002

For this lesson, the first thing we want to go over is defining operators; we've done it with methods; it's good to see it in action with classes, since this is where you would most likely be using the operators.0008

You can define all of the exact same operators.0027

Arithmetic operators: addition, subtraction, multiplication, division, modulus/remainder; unary operators--you can define the plus operator, the minus operator, and the increment and the decrement operator...0034

When I'm defining these operators...let's go over how to do it.0056

I have this class called Basket; what it does is: first, takes out initialize--you can initialize it with an Array of items; it holds a basket of items.0065

I have this method called initialize; the first parameter is just items, equal, and it's an empty Array by default; if you pass things in it, then it will instantiate this variable @items with whatever you give it.0078

Then, I have these new operators; we have an addition operator and a subtraction one.0096

It takes another object--I'm saying it's going to take this other object, this other "Basket," and it's going to take the items in this existing Basket and the ones from this new Basket, and it's going to add them together.0107

So you see, it creates this Basket.new, and you have your @items here, and it adds those with your other Basket.@items, too.0125

This is the same with the subtraction: instead, it's going to look at the items in this Basket and remove any items it sees in the Basket B.0137

To do that, we call @items, and then it says, from that parameter, to subtract any items that exist in that other Basket.0150

Let's go ahead and go through an example with this.0162

I'm going to call my terminal, my IRB, and just create the class first; I'm calling this class Basket.0166

I'm using the attribute reader, the getter method we saw in the first lesson.0181

Then, I'm going to initialize the items class, and then the next one is, we're going to look at that addition operator.0190

Here, I do my @items; that is the items that are in my current class; and then, I'm going to take that other class and add those items in, too.0216

Then, the same thing with the subtraction: I'm going to do the exact same thing, but instead, it's going to remove the items from this class.0227

There you go; I've created my class.0240

Let's first just do a basic instantiation; do a Basket.new; you will notice that it defaults to an empty Array of items.0243

That is no good for us; we actually want to set some values.0253

So, I'm going to create my Array; I'm going to put some grapes, some bananas, and some apples.0257

OK, so we've created our Basket; we have three items in here; I'm going to create another Basket, and I'm just going to point that to C.0267

For this one, I'm going to put some grapes, some apples...let's put some pineapple in there--how about that?0277

We have two Baskets now; we have three items in each one.0284

Let's go ahead and use our operator methods we just created.0290

I'm just going to call B+C; I have [grapes, bananas, apples]; I have [grapes, apples, bananas]; and we're going to put them all together in one Basket.0294

I call that, and now I have this new Basket that has six items in there.0305

Basically, using the code that we just created, we defined it, and you notice how easy it is: we just call B+C, and we've just created that method.0315

We can also use spaces; it works fine, too; it uses it normally, just like Ruby does for any of their Arrays...and String classes use that concatenate...you're doing the exact same thing now--you're defining that operator.0326

Same thing: I do B-C; now, I only get [bananas]; why is that?0340

For my first object, I have [grapes, bananas, apples]; in my second one, I also have grapes and apples in there, but I do have pineapple, but it doesn't need to worry about that.0347

But since grapes and apples are in that first Basket, it's going to remove them completely, so the only thing left in that Basket would be bananas.0359

That is how these operators work; you can define unary operators the exact same way.0366

Next, we want to look at class methods.0380

A class method is invoked by the class itself.0384

You don't have to actually instantiate any class; you don't have to call any constructor class method; you just call the class name, dot, whatever the method is, and that will create it.0392

So, this is invoked by prefixing the name of the method with the name of the class.0403

Here are some examples.0409

For the Math class, we have--we have many class methods for this--one is just Math.cos; that calls the cosine method.0412

Math.exp calls the exponent method; and then, we also have Array.new that calls the initialize value, which is an instance method from the Array.0423

But this will create the new class; so just Array.new will do that.0436

What happens now is...what I want to do is show you this class method in action.0441

Let's use a real-world example so you can see how it's defined in your class, too.0451

I have this class, Basket; first, remember: when I create this method, it's invoked by prefixing the name of the method with the name of the class.0455

This would be...you would have your class here, and then your method; this is how it's structured.0469

We're going to use that to create our method here.0480

Our method is going to add up all of the items together; it's going to create them all in a created new Basket, just like we do with the additional operator.0483

This is going to allow multiple Baskets to go in there.0492

Here is my class method: I just call define(Basket).add, and the parameter it's going to take is this asterisk sign of *Baskets.0496

It's going to take multiple Baskets, and what it's going to do is, it's going to create this new Basket.0505

In my code, I'm going to say, "OK, I'm going to create this new Array of items."0511

It's going to go through each of the Baskets, one by one; it's going to get each Basket and store all of the items in my local variable right here.0517

After it does that with all of the Baskets, we have all of these items in this local Array; then, it's going to say, "OK, I want you to flatten them; make them one Array; no multidimensional Arrays; and create a new Basket with that."0529

That is our class method at work.0547

Notice, here we are using this unique variable parameter that we discussed in our methods lesson0552

This is a variable argument parameter.0564

Otherwise, now we have our class method.0573

There is another way you could do it: if I have it already defined, I can actually add in the class method later.0578

That's what I'm going to show you right now: I call this opening up the class; this is another way to define class methods, and it adds an additional method to the existing code.0588

All you do is to call that class and just pass in that class name; so I'm going to pass in Basket.0602

This is going to do the exact same functionality as the other code; all this code is the same.0614

But notice, I didn't call Basket.at here; it just says define(add), takes a parameter called Baskets, and takes that Array of items; goes and gets all the items; and creates this new Basket.0622

This might be useful if I have a lot of these class methods; it saves you a little code--I don't have to add the class name in front of add.0638

It's good if you have existing code, and you just want it to find your class methods in it later.0649

It's probably good if I show you an example of this now.0654

Let's get the terminal here: OK, so from this example, we have our IRB terminal set up; we actually defined that basket method.0660

Instead of creating that whole thing from scratch, we can just add on to this basket method now, and create that class method.0670

All I do is just to--first clear it; it's still in memory, so--I'm using that < < that's going to allow me to put in that new class method.0679

Then, I'm going to pass Basket in here; then, I'm just going to do define_add_Baskets.0691

This is our class method; I'm going to declare the items in there, and then I'm going to call Baskets.each.0704

Then, I'm going to create my new Basket from all those items.0722

There you go: I had my existing Basket and added this new class method.0729

Now, let's see it in action; we're going to just get a couple of different Baskets.0735

Let's call one of them Fruits; I'm going to make this Basket.new; it's going to have grapes, strawberries, and almonds.0740

That is my Fruit Basket.0754

I'm going to make one of nuts: let's say I have peanuts, almonds...oh, I called it net instead of new.0756

Now, we have a Basket of that; now we have those two.0772

Now, I can just call my new class method, Basket.add, and I'm going to pass in those two Baskets.0778

We have something going on here...let's see, if I do Basket.add...undefined local variable...OK; I spelled it wrong here; I forgot that s.0803

Let me just declare that again; I'll show you where I made that mistake: here, I put Basket, but it should actually be Baskets.each.0819

This will fix that issue, and then we can continue on our way.0837

That is OK; so, we updated our class method; we still have our Fruits and our Nuts; I can just do Basket.add, Fruits, Nuts...0846

There you go; when I use my add class method, it creates this new method, and it adds them together.0859

I can also add multiple Baskets; so, let's do one--Meats--how about that?0869

I do Basket.new; let's say we have some chicken, some steak; so I have my Basket full of raw meat--it's not cooked yet.0879

Then, I can do Basket.add, put that in, and it's going to create a bigger Basket with everything in it.0894

So, we have grapes, strawberries from Fruits; almonds, peanuts from Nuts; and chicken and steak.0900

There you have it; we used this class method; we are able to use that with our other methods now, and it is still in the same scope for that class object, so we still get that benefit of keeping it simple--keeping it in the scope.0908

It's not part of any instance method, but we can still use it to simplify our code, so it's nice.0924

Next, let's look at the Array and Hash access method; this is very useful--you're not actually in an Array, but you can still use it and define it; Ruby gives you that flexibility.0941

To use it, use these square brackets; this allows you to define your own Hash access method in your class.0959

I have some code here; I just call it Triplex; it takes three values from the beginning, and then you can put that value out and manipulate it to do different things.0974

Let's go over this class; first, we have the attribute reader, so we can read the first, second, and third value from the method.0993

Then, I have this initialize method: it takes three values and creates those instance variables from them.1004

Then, here is where it gets interesting: we have that Hash access method here.1012

Here we have our Hash access method.1019

Notice, here we have the square brackets; it takes a parameter of index; so this is actually like manipulating an Array.1029

I can call the index values from it, and that is what it does; so, when I pass 0, it's going to get me that at first instance variable; 1, the at second; 2, the at third.1041

Let's see if we can take this code and show it to you.1055

Very essentially, it's going to call this triplex method, and it's going to call 0, 1, and 2, and show those values for it.1067

Let's go to our class, so you can see it.1083

Here you see the case index, (1,0), called first, (1,1) called second, (1,2) called at third.1096

Let me go over and show you the other part of that code that you just saw.1109

After this, we want to look at enumerating the values.1116

We are defining the each iterator here.1121

Since we don't have an object to loop, we can just use the yield in our method.1127

What we're going to is define that each iterator; I just called yield; it's going to get that first value, the @second, the @third.1134

Very quickly, you can see the code here: I called triplex.new(1,2,3); it calls each, and it's going to just print out those values: 1, 2, and 3.1153

The next thing we're going to look at is defining, testing for equality; so we're just defining this ==method operator here.1173

I put ... here, just for all the extra code; this is being defined below that.1185

Here I have this ==; it takes a parameter of other, which is another triplex object.1194

Here, I'm going to test for equality between these objects; if the three values are the same and they're both a Triplex object, it's going to say this is equal.1201

You can define them your own way in your own code, depending on what you're developing, but for this case, I'm going to say they have to be the same class type--that is why I put this method here.1210

This is a Triplex; I want to make sure; if it isn't, it's just going to return false--it's not equal.1224

Here we have our @first; it's going to look at these two values and make sure they're the same.1233

Make sure the second value is the same, and make sure the third value is the same.1240

Here, to test for equality, all you do is...you have your first object here--I want to show you this Triplex object--and my second one (the same value is in there); I'm going to choose this == value and test for operators; it says, "Hey, these are the same; this is true."1252

But notice, if I take this and just pass an Array in, it's going to be false, because this is an Array object, and it doesn't allow that through.1274

Plus, if I create a new one, and I put [2,1,3], that would be false; but also, if I were to just do == Triplex, and just pass [1,2,3] with the new, this would return true.1283

Let's see this code in action; let's go back to that code file we saw earlier.1305

First, we have the Hash method; we already talked about the each iterator; and then, we defined that == to test for equality.1313

The first thing I'm going to do is to run this whole class; it's going to instantiate a new object called Triplex; the first one is going to define three values, 1, 2, and 3.1334

Then, it's going to just access them, just like a Hash.1350

This first one is going to just print out 1; t1 will print out the second value, which is 2; t2 will print out the third value, which is 3.1356

Next, we're going to look at enumerating the values.1371

This is the one with the yield we're using up here.1376

That is going to print each number subsequently on one line, so it's going to be one line, 1; a new line, 2; a new line, 3.1382

Then, we're going to look at testing equality--just that code we just went through with the slides.1398

We're going to have our Triplex new value, [1,2,3]; we're going to create a new object with the exact same values.1405

It's going to--if it's ==, it's going to return true; we're going to check with different types--it should be false.1413

Then, passing it with the Triplex.new[2,1,3]--that would be false, also.1422

But, as I just showed you, we can validate that if we call Triplex.new and just do [1,2,3], and check if that returns true as we assume.1428

I'm just going to call my Ruby Triplex here, and notice we get our Hash access value [1,2,3].1445

We already defined it; now, we have our enumerating value, where we use that each method; we call that yield, [1,2,3]; and then, we're testing it for the equality, so first, we're instantiating those objects, then we test if they're equal.1456

It's going to be true; then we test that with an Array; we're going to get false; we test that with a new object, shifting the second and first value around--it should be false.1473

The last one is where we actually create a new object, and it says true there.1484

That is that; here is our new method here.1492

The last thing we want to look at is just constants.1500

We've already talked about constants in previous lessons, but you can also define them in your class, and it's very useful, because some constants are made just exclusively for that class itself--you want to define it outside in a global scope, then.1506

You want to keep things really organized, so you can create a constant in a class.1522

It's usually defined at the top of the class; that's just convention; but you can do it whatever way you want--whatever is most functional.1528

Outside the class, they must be prefixed by the name of the class; and that is very important.1535

Inside this class, if I call, I can just call 0 here, and that will get me my constant.1542

But, outside the class, I have to call this Triplex::0.1551

This will get me that constant value there.1560

For this constant, what it's actually doing is just to say that if I call Triplex::0, it's going to create for me a new object with none of the values initialized to anything other than 0--so first is going to be 0; second is going to be 0; and third is going to be 0; first, second, and third.1566

Otherwise, that is the end of this lesson on classes.1594

Hope to see you next time here at Educator.com!1599