Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Ruby Objects and Classes!
      
    
You have completed Ruby Objects and Classes!
Preview
    
      
  We're going to be creating a class that will approximate a bank account. In this first video, we'll lay the groundwork for our program and define the class and an internal variable.
Code Samples
class BankAccount
  def initialize(name)
    @name = name
    @transactions = []
  end
end
bank_account = BankAccount.new("Jason")
puts bank_account.inspect
              Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      [MUSIC]
                      0:00
                    
                    
                      So far we've learned about how to write
classes and methods.
                      0:04
                    
                    
                      We've also learned about the different
kinds of variables we can use in
                      0:09
                    
                    
                      our classes.
                      0:12
                    
                    
                      Let's put it all together and write the
simple bank account class.
                      0:14
                    
                    
                      We're going to be using all of the classes
we've learned about so far.
                      0:18
                    
                    
                      String, numbers, arrays and more.
                      0:22
                    
                    
                      We'll be doing all of this inside
workspaces, so let's get to it.
                      0:25
                    
                    
                      Okay, so let's go ahead and create our
bank account class.
                      0:30
                    
                    
                      So, let's go to the File > New File.
                      0:36
                    
                    
                      We're going to call this bank_account.rb.
                      0:41
                    
                    
                      Conventionally, we use all lowercase
letters and
                      0:48
                    
                    
                      where there would be a space, we put an
underscore.
                      0:52
                    
                    
                      So now, let's write our bank account
class.
                      0:56
                    
                    
                      We start out by defining a class using the
class keyword.
                      1:00
                    
                    
                      And then, we type the name of the class
that we want.
                      1:05
                    
                    
                      In this case, it's BankAccount.
                      1:08
                    
                    
                      [BLANK_AUDIO]
                      1:10
                    
                    
                      When naming classes, we have to
                      1:12
                    
                    
                      have a capital letter as the first part of
the class name.
                      1:16
                    
                    
                      And then, if we have more than one word in
the class name,
                      1:23
                    
                    
                      like we do with BankAccount, we make that
second word have a capital letter as well.
                      1:26
                    
                    
                      This isn't strictly necessary, but
                      1:32
                    
                    
                      it is how a lot of Ruby programmers write
Ruby code.
                      1:35
                    
                    
                      So it's important to follow these
conventions.
                      1:39
                    
                    
                      Now, let's go ahead and define the
initialize method,
                      1:43
                    
                    
                      which gets called when we instantiate our
class.
                      1:47
                    
                    
                      Now let's think a little bit about what we
need inside of a bank account.
                      1:51
                    
                    
                      We need to know who the account belongs
to.
                      1:56
                    
                    
                      So, let's go ahead and have a name
attribute in our bank account.
                      1:59
                    
                    
                      And we'll send that in when we create the
account.
                      2:05
                    
                    
                      [SOUND] Now, our bank account is
                      2:08
                    
                    
                      also going to have transactions.
                      2:13
                    
                    
                      The transactions are going to be all of
the debits and
                      2:18
                    
                    
                      credits that occur on the account.
                      2:23
                    
                    
                      We can use an array to take care of
holding that information.
                      2:26
                    
                    
                      Therefore, when we start this bank
account,
                      2:31
                    
                    
                      let's go ahead and create an empty array
of transactions.
                      2:34
                    
                    
                      Now let's go ahead and initialize our bank
account and see what it looks like.
                      2:41
                    
                    
                      [SOUND] And let's go ahead and
                      2:45
                    
                    
                      print out the bank account instance now.
                      2:50
                    
                    
                      And run inspect on it just to see what it
looks like.
                      2:57
                    
                    
                      [SOUND] Okay, we can see we have a new
BankAccount class,
                      3:00
                    
                    
                      has a name, and an empty array of
transactions.
                      3:07
                    
                    
                      So what we've done here, is we instantiate
a new instance of
                      3:13
                    
                    
                      the BankAccount class and assign it 
to this bank_account variable.
                      3:19
                    
                    
                      When we define this method,
                      3:25
                    
                    
                      the initialize method, this gets called
when we instantiate the instance.
                      3:27
                    
                    
                      So calling new will instantiate an
instance of the bank account and
                      3:34
                    
                    
                      run this method.
                      3:39
                    
                    
                      We defined an argument called name here
which we set to an instance variable.
                      3:41
                    
                    
                      Therefore, we have to send in a name when
we
                      3:48
                    
                    
                      instantiate the new instance of the bank
account.
                      3:52
                    
                    
                      We then have an empty array of
transactions which we will begin to
                      3:55
                    
                    
                      fill out in the next video.
                      3:59
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up