1 00:00:00,055 --> 00:00:04,860 [MUSIC] 2 00:00:04,860 --> 00:00:09,170 So far we've learned about how to write classes and methods. 3 00:00:09,170 --> 00:00:12,380 We've also learned about the different kinds of variables we can use in 4 00:00:12,380 --> 00:00:14,000 our classes. 5 00:00:14,000 --> 00:00:18,230 Let's put it all together and write the simple bank account class. 6 00:00:18,230 --> 00:00:22,120 We're going to be using all of the classes we've learned about so far. 7 00:00:22,120 --> 00:00:24,630 String, numbers, arrays and more. 8 00:00:25,700 --> 00:00:29,480 We'll be doing all of this inside workspaces, so let's get to it. 9 00:00:30,745 --> 00:00:35,080 Okay, so let's go ahead and create our bank account class. 10 00:00:36,820 --> 00:00:40,400 So, let's go to the File > New File. 11 00:00:41,820 --> 00:00:48,470 We're going to call this bank_account.rb. 12 00:00:48,470 --> 00:00:52,160 Conventionally, we use all lowercase letters and 13 00:00:52,160 --> 00:00:54,870 where there would be a space, we put an underscore. 14 00:00:56,060 --> 00:00:59,330 So now, let's write our bank account class. 15 00:01:00,330 --> 00:01:05,170 We start out by defining a class using the class keyword. 16 00:01:05,170 --> 00:01:07,480 And then, we type the name of the class that we want. 17 00:01:08,820 --> 00:01:10,827 In this case, it's BankAccount. 18 00:01:10,827 --> 00:01:12,962 [BLANK_AUDIO] 19 00:01:12,962 --> 00:01:16,082 When naming classes, we have to 20 00:01:16,082 --> 00:01:21,640 have a capital letter as the first part of the class name. 21 00:01:23,360 --> 00:01:26,720 And then, if we have more than one word in the class name, 22 00:01:26,720 --> 00:01:32,870 like we do with BankAccount, we make that second word have a capital letter as well. 23 00:01:32,870 --> 00:01:35,270 This isn't strictly necessary, but 24 00:01:35,270 --> 00:01:39,900 it is how a lot of Ruby programmers write Ruby code. 25 00:01:39,900 --> 00:01:43,170 So it's important to follow these conventions. 26 00:01:43,170 --> 00:01:47,420 Now, let's go ahead and define the initialize method, 27 00:01:47,420 --> 00:01:51,590 which gets called when we instantiate our class. 28 00:01:51,590 --> 00:01:56,080 Now let's think a little bit about what we need inside of a bank account. 29 00:01:56,080 --> 00:01:59,870 We need to know who the account belongs to. 30 00:01:59,870 --> 00:02:04,320 So, let's go ahead and have a name attribute in our bank account. 31 00:02:05,440 --> 00:02:08,125 And we'll send that in when we create the account. 32 00:02:08,125 --> 00:02:13,186 [SOUND] Now, our bank account is 33 00:02:13,186 --> 00:02:18,837 also going to have transactions. 34 00:02:18,837 --> 00:02:23,150 The transactions are going to be all of the debits and 35 00:02:23,150 --> 00:02:26,970 credits that occur on the account. 36 00:02:26,970 --> 00:02:31,580 We can use an array to take care of holding that information. 37 00:02:31,580 --> 00:02:34,570 Therefore, when we start this bank account, 38 00:02:34,570 --> 00:02:38,340 let's go ahead and create an empty array of transactions. 39 00:02:41,700 --> 00:02:45,325 Now let's go ahead and initialize our bank account and see what it looks like. 40 00:02:45,325 --> 00:02:50,142 [SOUND] And let's go ahead and 41 00:02:50,142 --> 00:02:57,174 print out the bank account instance now. 42 00:02:57,174 --> 00:03:00,778 And run inspect on it just to see what it looks like. 43 00:03:00,778 --> 00:03:07,650 [SOUND] Okay, we can see we have a new BankAccount class, 44 00:03:07,650 --> 00:03:13,660 has a name, and an empty array of transactions. 45 00:03:13,660 --> 00:03:19,091 So what we've done here, is we instantiate a new instance of 46 00:03:19,091 --> 00:03:25,620 the BankAccount class and assign it to this bank_account variable. 47 00:03:25,620 --> 00:03:27,540 When we define this method, 48 00:03:27,540 --> 00:03:32,890 the initialize method, this gets called when we instantiate the instance. 49 00:03:34,258 --> 00:03:39,570 So calling new will instantiate an instance of the bank account and 50 00:03:39,570 --> 00:03:40,350 run this method. 51 00:03:41,790 --> 00:03:47,340 We defined an argument called name here which we set to an instance variable. 52 00:03:48,610 --> 00:03:52,170 Therefore, we have to send in a name when we 53 00:03:52,170 --> 00:03:55,790 instantiate the new instance of the bank account. 54 00:03:55,790 --> 00:03:59,630 We then have an empty array of transactions which we will begin to 55 00:03:59,630 --> 00:04:01,370 fill out in the next video.